Skip to content

Commit 8e88547

Browse files
Tweak error code for sized checks of const/static
1 parent 71e06b9 commit 8e88547

File tree

12 files changed

+26
-7
lines changed

12 files changed

+26
-7
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,13 @@ fn check_associated_item(
11081108
let ty = tcx.type_of(item.def_id).instantiate_identity();
11091109
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
11101110
wfcx.register_wf_obligation(span, loc, ty.into());
1111-
check_sized_if_body(wfcx, item.def_id.expect_local(), ty, Some(span));
1111+
check_sized_if_body(
1112+
wfcx,
1113+
item.def_id.expect_local(),
1114+
ty,
1115+
Some(span),
1116+
ObligationCauseCode::SizedConstOrStatic,
1117+
);
11121118
Ok(())
11131119
}
11141120
ty::AssocKind::Fn => {
@@ -1354,7 +1360,7 @@ fn check_item_type(
13541360
traits::ObligationCause::new(
13551361
ty_span,
13561362
wfcx.body_def_id,
1357-
ObligationCauseCode::WellFormed(None),
1363+
ObligationCauseCode::SizedConstOrStatic,
13581364
),
13591365
wfcx.param_env,
13601366
item_ty,
@@ -1698,6 +1704,7 @@ fn check_fn_or_method<'tcx>(
16981704
hir::FnRetTy::Return(ty) => Some(ty.span),
16991705
hir::FnRetTy::DefaultReturn(_) => None,
17001706
},
1707+
ObligationCauseCode::SizedReturnType,
17011708
);
17021709
}
17031710

@@ -1706,13 +1713,14 @@ fn check_sized_if_body<'tcx>(
17061713
def_id: LocalDefId,
17071714
ty: Ty<'tcx>,
17081715
maybe_span: Option<Span>,
1716+
code: ObligationCauseCode<'tcx>,
17091717
) {
17101718
let tcx = wfcx.tcx();
17111719
if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
17121720
let span = maybe_span.unwrap_or(body.value.span);
17131721

17141722
wfcx.register_bound(
1715-
ObligationCause::new(span, def_id, traits::ObligationCauseCode::SizedReturnType),
1723+
ObligationCause::new(span, def_id, code),
17161724
wfcx.param_env,
17171725
ty,
17181726
tcx.require_lang_item(LangItem::Sized, Some(span)),

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18101810
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
18111811

18121812
let ty = fcx.check_expr_with_expectation(body.value, expected);
1813-
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized);
1813+
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
18141814
fcx.write_ty(block.hir_id, ty);
18151815
ty
18161816
}

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ pub enum ObligationCauseCode<'tcx> {
274274
},
275275

276276
/// Constant expressions must be sized.
277-
ConstSized,
277+
SizedConstOrStatic,
278278

279279
/// `static` items must have `Sync` type.
280280
SharedStatic,

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3125,8 +3125,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
31253125
Applicability::MachineApplicable,
31263126
);
31273127
}
3128-
ObligationCauseCode::ConstSized => {
3129-
err.note("constant expressions must have a statically known size");
3128+
ObligationCauseCode::SizedConstOrStatic => {
3129+
err.note("statics and constants must have a statically known size");
31303130
}
31313131
ObligationCauseCode::InlineAsmSized => {
31323132
err.note("all inline asm arguments must have a statically known size");

tests/ui/consts/const-slice-array-deref.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | const ONE: [u16] = [1];
55
| ^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u16]`
8+
= note: statics and constants must have a statically known size
89

910
error[E0308]: mismatched types
1011
--> $DIR/const-slice-array-deref.rs:1:20

tests/ui/consts/const-unsized.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
55
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
8+
= note: statics and constants must have a statically known size
89

910
error[E0277]: the size for values of type `str` cannot be known at compilation time
1011
--> $DIR/const-unsized.rs:7:18
@@ -13,6 +14,7 @@ LL | const CONST_FOO: str = *"foo";
1314
| ^^^ doesn't have a size known at compile-time
1415
|
1516
= help: the trait `Sized` is not implemented for `str`
17+
= note: statics and constants must have a statically known size
1618

1719
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
1820
--> $DIR/const-unsized.rs:11:18
@@ -21,6 +23,7 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
2123
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2224
|
2325
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
26+
= note: statics and constants must have a statically known size
2427

2528
error[E0277]: the size for values of type `str` cannot be known at compilation time
2629
--> $DIR/const-unsized.rs:15:20
@@ -29,6 +32,7 @@ LL | static STATIC_BAR: str = *"bar";
2932
| ^^^ doesn't have a size known at compile-time
3033
|
3134
= help: the trait `Sized` is not implemented for `str`
35+
= note: statics and constants must have a statically known size
3236

3337
error[E0507]: cannot move out of a shared reference
3438
--> $DIR/const-unsized.rs:3:35

tests/ui/consts/const_refs_to_static-ice-121413.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LL | static FOO: Sync = AtomicUsize::new(0);
3030
| ^^^^ doesn't have a size known at compile-time
3131
|
3232
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
33+
= note: statics and constants must have a statically known size
3334

3435
error: aborting due to 2 previous errors; 1 warning emitted
3536

tests/ui/extern/issue-36122-accessing-externed-dst.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static symbol: [usize];
55
| ^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[usize]`
8+
= note: statics and constants must have a statically known size
89

910
error[E0133]: use of extern static is unsafe and requires unsafe function or block
1011
--> $DIR/issue-36122-accessing-externed-dst.rs:5:20

tests/ui/issues/issue-54410.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | pub static mut symbol: [i8];
55
| ^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[i8]`
8+
= note: statics and constants must have a statically known size
89

910
error: aborting due to 1 previous error
1011

tests/ui/static/issue-24446.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | static foo: dyn Fn() -> u32 = || -> u32 {
1414
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
1515
|
1616
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
17+
= note: statics and constants must have a statically known size
1718

1819
error[E0308]: mismatched types
1920
--> $DIR/issue-24446.rs:2:35

tests/ui/statics/unsized_type2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: required because it appears within the type `Foo`
1010
|
1111
LL | pub struct Foo {
1212
| ^^^
13+
= note: statics and constants must have a statically known size
1314

1415
error[E0308]: mismatched types
1516
--> $DIR/unsized_type2.rs:14:45

tests/ui/trait-bounds/ice-unsized-tuple-const-issue-121443.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | const TEST: Fn = some_fn;
1111
| ^^ doesn't have a size known at compile-time
1212
|
1313
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
14+
= note: statics and constants must have a statically known size
1415

1516
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
1617
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:11:14

0 commit comments

Comments
 (0)