Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9183270

Browse files
committed
Auto merge of rust-lang#137233 - compiler-errors:sized, r=<try>
Unconditionally check sizedness of body in typeck to taint typeck results Puts the sized check back into typeck (duplicated -- we still have it in wfcheck) so that we properly taint bodies. These duplicated diagnostics will all be deduplicated with `-Zdeduplicate-diagnostics`, so it's just UI test fallout and not an actual regression in user-facing diagnostics. We could perhaps do this without duplicating all the diagnostics if we put this check into something like mir build 🤔 Ideally CTFE would just be more fallible to non-WF code but 🤷 Fixes rust-lang#137186
2 parents 5986ff0 + a0dbaf2 commit 9183270

File tree

56 files changed

+799
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+799
-160
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,13 @@ fn check_associated_item(
11101110
let ty = tcx.type_of(item.def_id).instantiate_identity();
11111111
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
11121112
wfcx.register_wf_obligation(span, loc, ty.into());
1113-
check_sized_if_body(wfcx, item.def_id.expect_local(), ty, Some(span));
1113+
check_sized_if_body(
1114+
wfcx,
1115+
item.def_id.expect_local(),
1116+
ty,
1117+
Some(span),
1118+
ObligationCauseCode::SizedConstOrStatic,
1119+
);
11141120
Ok(())
11151121
}
11161122
ty::AssocKind::Fn => {
@@ -1356,7 +1362,7 @@ fn check_item_type(
13561362
traits::ObligationCause::new(
13571363
ty_span,
13581364
wfcx.body_def_id,
1359-
ObligationCauseCode::WellFormed(None),
1365+
ObligationCauseCode::SizedConstOrStatic,
13601366
),
13611367
wfcx.param_env,
13621368
item_ty,
@@ -1700,6 +1706,7 @@ fn check_fn_or_method<'tcx>(
17001706
hir::FnRetTy::Return(ty) => Some(ty.span),
17011707
hir::FnRetTy::DefaultReturn(_) => None,
17021708
},
1709+
ObligationCauseCode::SizedReturnType,
17031710
);
17041711
}
17051712

@@ -1708,13 +1715,14 @@ fn check_sized_if_body<'tcx>(
17081715
def_id: LocalDefId,
17091716
ty: Ty<'tcx>,
17101717
maybe_span: Option<Span>,
1718+
code: ObligationCauseCode<'tcx>,
17111719
) {
17121720
let tcx = wfcx.tcx();
17131721
if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
17141722
let span = maybe_span.unwrap_or(body.value.span);
17151723

17161724
wfcx.register_bound(
1717-
ObligationCause::new(span, def_id, traits::ObligationCauseCode::SizedReturnType),
1725+
ObligationCause::new(span, def_id, code),
17181726
wfcx.param_env,
17191727
ty,
17201728
tcx.require_lang_item(LangItem::Sized, Some(span)),

compiler/rustc_hir_typeck/src/check.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,16 @@ pub(super) fn check_fn<'a, 'tcx>(
113113

114114
fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
115115

116-
// We checked the root's ret ty during wfcheck, but not the child.
117-
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
118-
let return_or_body_span = match decl.output {
119-
hir::FnRetTy::DefaultReturn(_) => body.value.span,
120-
hir::FnRetTy::Return(ty) => ty.span,
121-
};
122-
123-
fcx.require_type_is_sized(
124-
declared_ret_ty,
125-
return_or_body_span,
126-
ObligationCauseCode::SizedReturnType,
127-
);
128-
}
116+
let return_or_body_span = match decl.output {
117+
hir::FnRetTy::DefaultReturn(_) => body.value.span,
118+
hir::FnRetTy::Return(ty) => ty.span,
119+
};
120+
121+
fcx.require_type_is_sized(
122+
declared_ret_ty,
123+
return_or_body_span,
124+
ObligationCauseCode::SizedReturnType,
125+
);
129126

130127
fcx.is_whole_body.set(true);
131128
fcx.check_return_or_body_tail(body.value, false);

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
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_hir_typeck/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ fn typeck_with_inspect<'tcx>(
185185

186186
let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id)));
187187
fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code);
188+
fcx.require_type_is_sized(
189+
expected_type,
190+
node.ty().map_or(body.value.span, |ty| ty.span),
191+
ObligationCauseCode::SizedConstOrStatic,
192+
);
188193

189194
// Gather locals in statics (because of block expressions).
190195
GatherLocalsVisitor::new(&fcx).visit_body(body);

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub enum ObligationCauseCode<'tcx> {
273273
},
274274

275275
/// Constant expressions must be sized.
276-
ConstSized,
276+
SizedConstOrStatic,
277277

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

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

Lines changed: 2 additions & 2 deletions
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/associated-consts/issue-58022.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl Bar<[u8]> {
1212

1313
fn new(slice: &[u8; Self::SIZE]) -> Self {
1414
//~^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
15+
//~| ERROR: the size for values of type `[u8]` cannot be known at compilation time
1516
Foo(Box::new(*slice))
1617
//~^ ERROR: expected function, tuple struct or tuple variant, found trait `Foo`
1718
}

tests/ui/associated-consts/issue-58022.stderr

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@ LL |
2121
LL | fn new(slice: &[u8; Foo::SIZE]) -> Self;
2222
| ^^^^^^^^^ cannot refer to the associated constant of trait
2323

24+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
25+
--> $DIR/issue-58022.rs:13:41
26+
|
27+
LL | fn new(slice: &[u8; Self::SIZE]) -> Self {
28+
| ^^^^ doesn't have a size known at compile-time
29+
|
30+
= help: within `Bar<[u8]>`, the trait `Sized` is not implemented for `[u8]`
31+
note: required because it appears within the type `Bar<[u8]>`
32+
--> $DIR/issue-58022.rs:8:12
33+
|
34+
LL | pub struct Bar<T: ?Sized>(T);
35+
| ^^^
36+
= note: the return type of a function must have a statically known size
37+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
38+
2439
error[E0423]: expected function, tuple struct or tuple variant, found trait `Foo`
25-
--> $DIR/issue-58022.rs:15:9
40+
--> $DIR/issue-58022.rs:16:9
2641
|
2742
LL | Foo(Box::new(*slice))
2843
| ^^^ not a function, tuple struct or tuple variant
2944

30-
error: aborting due to 3 previous errors
45+
error: aborting due to 4 previous errors
3146

3247
Some errors have detailed explanations: E0277, E0423, E0790.
3348
For more information about an error, try `rustc --explain E0277`.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const ONE: [u16] = [1];
22
//~^ ERROR the size for values of type `[u16]` cannot be known at compilation time
3+
//~| ERROR the size for values of type `[u16]` cannot be known at compilation time
34
//~| ERROR mismatched types
45

56
const TWO: &'static u16 = &ONE[0];

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@ 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
1112
|
1213
LL | const ONE: [u16] = [1];
1314
| ^^^ expected `[u16]`, found `[u16; 1]`
1415

16+
error[E0277]: the size for values of type `[u16]` cannot be known at compilation time
17+
--> $DIR/const-slice-array-deref.rs:1:12
18+
|
19+
LL | const ONE: [u16] = [1];
20+
| ^^^^^ doesn't have a size known at compile-time
21+
|
22+
= help: the trait `Sized` is not implemented for `[u16]`
23+
= note: statics and constants must have a statically known size
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25+
1526
error[E0161]: cannot move a value of type `[u16]`
16-
--> $DIR/const-slice-array-deref.rs:5:28
27+
--> $DIR/const-slice-array-deref.rs:6:28
1728
|
1829
LL | const TWO: &'static u16 = &ONE[0];
1930
| ^^^ the size of `[u16]` cannot be statically determined
2031

21-
error: aborting due to 3 previous errors
32+
error: aborting due to 4 previous errors
2233

2334
Some errors have detailed explanations: E0161, E0277, E0308.
2435
For more information about an error, try `rustc --explain E0161`.

0 commit comments

Comments
 (0)