1
Fork 0

Tweak error code for sized checks of const/static

This commit is contained in:
Michael Goulet 2025-02-18 17:11:12 +00:00
parent e16a049adb
commit c566318a78
12 changed files with 26 additions and 7 deletions

View file

@ -1108,7 +1108,13 @@ fn check_associated_item(
let ty = tcx.type_of(item.def_id).instantiate_identity(); let ty = tcx.type_of(item.def_id).instantiate_identity();
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty); let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
wfcx.register_wf_obligation(span, loc, ty.into()); wfcx.register_wf_obligation(span, loc, ty.into());
check_sized_if_body(wfcx, item.def_id.expect_local(), ty, Some(span)); check_sized_if_body(
wfcx,
item.def_id.expect_local(),
ty,
Some(span),
ObligationCauseCode::SizedConstOrStatic,
);
Ok(()) Ok(())
} }
ty::AssocKind::Fn => { ty::AssocKind::Fn => {
@ -1354,7 +1360,7 @@ fn check_item_type(
traits::ObligationCause::new( traits::ObligationCause::new(
ty_span, ty_span,
wfcx.body_def_id, wfcx.body_def_id,
ObligationCauseCode::WellFormed(None), ObligationCauseCode::SizedConstOrStatic,
), ),
wfcx.param_env, wfcx.param_env,
item_ty, item_ty,
@ -1698,6 +1704,7 @@ fn check_fn_or_method<'tcx>(
hir::FnRetTy::Return(ty) => Some(ty.span), hir::FnRetTy::Return(ty) => Some(ty.span),
hir::FnRetTy::DefaultReturn(_) => None, hir::FnRetTy::DefaultReturn(_) => None,
}, },
ObligationCauseCode::SizedReturnType,
); );
} }
@ -1706,13 +1713,14 @@ fn check_sized_if_body<'tcx>(
def_id: LocalDefId, def_id: LocalDefId,
ty: Ty<'tcx>, ty: Ty<'tcx>,
maybe_span: Option<Span>, maybe_span: Option<Span>,
code: ObligationCauseCode<'tcx>,
) { ) {
let tcx = wfcx.tcx(); let tcx = wfcx.tcx();
if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) { if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
let span = maybe_span.unwrap_or(body.value.span); let span = maybe_span.unwrap_or(body.value.span);
wfcx.register_bound( wfcx.register_bound(
ObligationCause::new(span, def_id, traits::ObligationCauseCode::SizedReturnType), ObligationCause::new(span, def_id, code),
wfcx.param_env, wfcx.param_env,
ty, ty,
tcx.require_lang_item(LangItem::Sized, Some(span)), tcx.require_lang_item(LangItem::Sized, Some(span)),

View file

@ -1806,7 +1806,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
crate::GatherLocalsVisitor::new(&fcx).visit_body(body); crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
let ty = fcx.check_expr_with_expectation(body.value, expected); let ty = fcx.check_expr_with_expectation(body.value, expected);
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized); fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
fcx.write_ty(block.hir_id, ty); fcx.write_ty(block.hir_id, ty);
ty ty
} }

View file

@ -266,7 +266,7 @@ pub enum ObligationCauseCode<'tcx> {
}, },
/// Constant expressions must be sized. /// Constant expressions must be sized.
ConstSized, SizedConstOrStatic,
/// `static` items must have `Sync` type. /// `static` items must have `Sync` type.
SharedStatic, SharedStatic,

View file

@ -3126,8 +3126,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
ObligationCauseCode::ConstSized => { ObligationCauseCode::SizedConstOrStatic => {
err.note("constant expressions must have a statically known size"); err.note("statics and constants must have a statically known size");
} }
ObligationCauseCode::InlineAsmSized => { ObligationCauseCode::InlineAsmSized => {
err.note("all inline asm arguments must have a statically known size"); err.note("all inline asm arguments must have a statically known size");

View file

@ -5,6 +5,7 @@ LL | const ONE: [u16] = [1];
| ^^^^^ doesn't have a size known at compile-time | ^^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `[u16]` = help: the trait `Sized` is not implemented for `[u16]`
= note: statics and constants must have a statically known size
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/const-slice-array-deref.rs:1:20 --> $DIR/const-slice-array-deref.rs:1:20

View file

@ -5,6 +5,7 @@ LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)` = help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `str` cannot be known at compilation time error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:7:18 --> $DIR/const-unsized.rs:7:18
@ -13,6 +14,7 @@ LL | const CONST_FOO: str = *"foo";
| ^^^ doesn't have a size known at compile-time | ^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `str` = help: the trait `Sized` is not implemented for `str`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:11:18 --> $DIR/const-unsized.rs:11:18
@ -21,6 +23,7 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)` = help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `str` cannot be known at compilation time error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:15:20 --> $DIR/const-unsized.rs:15:20
@ -29,6 +32,7 @@ LL | static STATIC_BAR: str = *"bar";
| ^^^ doesn't have a size known at compile-time | ^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `str` = help: the trait `Sized` is not implemented for `str`
= note: statics and constants must have a statically known size
error[E0507]: cannot move out of a shared reference error[E0507]: cannot move out of a shared reference
--> $DIR/const-unsized.rs:3:35 --> $DIR/const-unsized.rs:3:35

View file

@ -30,6 +30,7 @@ LL | static FOO: Sync = AtomicUsize::new(0);
| ^^^^ doesn't have a size known at compile-time | ^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)` = help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
= note: statics and constants must have a statically known size
error: aborting due to 2 previous errors; 1 warning emitted error: aborting due to 2 previous errors; 1 warning emitted

View file

@ -5,6 +5,7 @@ LL | static symbol: [usize];
| ^^^^^^^ doesn't have a size known at compile-time | ^^^^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `[usize]` = help: the trait `Sized` is not implemented for `[usize]`
= note: statics and constants must have a statically known size
error[E0133]: use of extern static is unsafe and requires unsafe function or block error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-36122-accessing-externed-dst.rs:5:20 --> $DIR/issue-36122-accessing-externed-dst.rs:5:20

View file

@ -5,6 +5,7 @@ LL | pub static mut symbol: [i8];
| ^^^^ doesn't have a size known at compile-time | ^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `[i8]` = help: the trait `Sized` is not implemented for `[i8]`
= note: statics and constants must have a statically known size
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -14,6 +14,7 @@ LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)` = help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
= note: statics and constants must have a statically known size
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-24446.rs:2:35 --> $DIR/issue-24446.rs:2:35

View file

@ -10,6 +10,7 @@ note: required because it appears within the type `Foo`
| |
LL | pub struct Foo { LL | pub struct Foo {
| ^^^ | ^^^
= note: statics and constants must have a statically known size
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/unsized_type2.rs:14:45 --> $DIR/unsized_type2.rs:14:45

View file

@ -11,6 +11,7 @@ LL | const TEST: Fn = some_fn;
| ^^ doesn't have a size known at compile-time | ^^ doesn't have a size known at compile-time
| |
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)` = help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
= note: statics and constants must have a statically known size
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:11:14 --> $DIR/ice-unsized-tuple-const-issue-121443.rs:11:14