Skip to content

Commit 4282576

Browse files
committed
Auto merge of rust-lang#122078 - gurry:121443-ice-layout-is-sized-alt, r=oli-obk
Check that return type is WF in typeck Ensures that non-WF types do not pass typeck and then later ICE in MIR/const eval Fixes rust-lang#121443
2 parents 1b2c53a + ace4367 commit 4282576

13 files changed

+168
-8
lines changed

compiler/rustc_hir_typeck/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_hir::{HirIdMap, Node};
6161
use rustc_hir_analysis::astconv::AstConv;
6262
use rustc_hir_analysis::check::check_abi;
6363
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
64-
use rustc_infer::traits::ObligationInspector;
64+
use rustc_infer::traits::{ObligationCauseCode, ObligationInspector, WellFormedLoc};
6565
use rustc_middle::query::Providers;
6666
use rustc_middle::traits;
6767
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -253,6 +253,10 @@ fn typeck_with_fallback<'tcx>(
253253
let expected_type = expected_type.unwrap_or_else(fallback);
254254

255255
let expected_type = fcx.normalize(body.value.span, expected_type);
256+
257+
let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id)));
258+
fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code);
259+
256260
fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);
257261

258262
// Gather locals in statics (because of block expressions).

tests/ui/const-generics/issues/issue-71202.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<T: Copy> DataHolder<T> {
2525
}
2626

2727
<IsCopy<T>>::VALUE
28-
} as usize] = [];
28+
} as usize] = []; //~ ERROR unconstrained generic constant
2929
}
3030

3131
fn main() {}

tests/ui/const-generics/issues/issue-71202.stderr

+26-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,30 @@ LL | | } as usize] = [];
2929
<IsCopy<T>>::VALUE
3030
} as usize]:`
3131

32-
error: aborting due to 1 previous error
32+
error: unconstrained generic constant
33+
--> $DIR/issue-71202.rs:28:19
34+
|
35+
LL | } as usize] = [];
36+
| ^^
37+
|
38+
= help: try adding a `where` bound using this expression: `where [(); 1 - {
39+
trait NotCopy {
40+
const VALUE: bool = false;
41+
}
42+
43+
impl<__Type: ?Sized> NotCopy for __Type {}
44+
45+
struct IsCopy<__Type: ?Sized>(PhantomData<__Type>);
46+
47+
impl<__Type> IsCopy<__Type>
48+
where
49+
__Type: Sized + Copy,
50+
{
51+
const VALUE: bool = true;
52+
}
53+
54+
<IsCopy<T>>::VALUE
55+
} as usize]:`
56+
57+
error: aborting due to 2 previous errors
3358

tests/ui/layout/issue-84108.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ const BAR: (&Path, [u8], usize) = ("hello", [], 42);
1111

1212
static BAZ: ([u8], usize) = ([], 0);
1313
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
14+
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
1415
//~| ERROR mismatched types

tests/ui/layout/issue-84108.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ LL | static BAZ: ([u8], usize) = ([], 0);
2929
= help: the trait `Sized` is not implemented for `[u8]`
3030
= note: only the last element of a tuple may have a dynamically sized type
3131

32+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
33+
--> $DIR/issue-84108.rs:12:13
34+
|
35+
LL | static BAZ: ([u8], usize) = ([], 0);
36+
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
37+
|
38+
= help: the trait `Sized` is not implemented for `[u8]`
39+
= note: only the last element of a tuple may have a dynamically sized type
40+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
41+
3242
error[E0308]: mismatched types
3343
--> $DIR/issue-84108.rs:12:30
3444
|
@@ -38,7 +48,7 @@ LL | static BAZ: ([u8], usize) = ([], 0);
3848
= note: expected slice `[u8]`
3949
found array `[_; 0]`
4050

41-
error: aborting due to 4 previous errors
51+
error: aborting due to 5 previous errors
4252

4353
Some errors have detailed explanations: E0277, E0308, E0412.
4454
For more information about an error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for #121443
2+
// Checks that no ICE occurs upon encountering
3+
// a tuple with unsized element that is not
4+
// the last element
5+
6+
type Fn = dyn FnOnce() -> u8;
7+
8+
const TEST: Fn = some_fn;
9+
//~^ ERROR cannot find value `some_fn` in this scope
10+
//~| ERROR the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
11+
//~| ERROR the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
12+
const TEST2: (Fn, u8) = (TEST, 0);
13+
//~^ ERROR the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
14+
//~| ERROR the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0425]: cannot find value `some_fn` in this scope
2+
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:8:18
3+
|
4+
LL | const TEST: Fn = some_fn;
5+
| ^^^^^^^ not found in this scope
6+
7+
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
8+
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:8:13
9+
|
10+
LL | const TEST: Fn = some_fn;
11+
| ^^ doesn't have a size known at compile-time
12+
|
13+
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
14+
15+
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
16+
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:8:18
17+
|
18+
LL | const TEST: Fn = some_fn;
19+
| ^^^^^^^ doesn't have a size known at compile-time
20+
|
21+
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
22+
= note: constant expressions must have a statically known size
23+
24+
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
25+
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:12:14
26+
|
27+
LL | const TEST2: (Fn, u8) = (TEST, 0);
28+
| ^^^^^^^^ doesn't have a size known at compile-time
29+
|
30+
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
31+
= note: only the last element of a tuple may have a dynamically sized type
32+
33+
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
34+
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:12:25
35+
|
36+
LL | const TEST2: (Fn, u8) = (TEST, 0);
37+
| ^^^^^^^^^ doesn't have a size known at compile-time
38+
|
39+
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
40+
= note: only the last element of a tuple may have a dynamically sized type
41+
42+
error: aborting due to 5 previous errors
43+
44+
Some errors have detailed explanations: E0277, E0425.
45+
For more information about an error, try `rustc --explain E0277`.

tests/ui/traits/bound/on-structs-and-enums-static.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct Foo<T:Trait> {
88

99
static X: Foo<usize> = Foo {
1010
//~^ ERROR E0277
11+
//~| ERROR E0277
1112
x: 1, //~ ERROR: E0277
1213
};
1314

tests/ui/traits/bound/on-structs-and-enums-static.stderr

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,25 @@ LL | struct Foo<T:Trait> {
1616
| ^^^^^ required by this bound in `Foo`
1717

1818
error[E0277]: the trait bound `usize: Trait` is not satisfied
19-
--> $DIR/on-structs-and-enums-static.rs:11:8
19+
--> $DIR/on-structs-and-enums-static.rs:9:11
20+
|
21+
LL | static X: Foo<usize> = Foo {
22+
| ^^^^^^^^^^ the trait `Trait` is not implemented for `usize`
23+
|
24+
help: this trait has no implementations, consider adding one
25+
--> $DIR/on-structs-and-enums-static.rs:1:1
26+
|
27+
LL | trait Trait {
28+
| ^^^^^^^^^^^
29+
note: required by a bound in `Foo`
30+
--> $DIR/on-structs-and-enums-static.rs:5:14
31+
|
32+
LL | struct Foo<T:Trait> {
33+
| ^^^^^ required by this bound in `Foo`
34+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
35+
36+
error[E0277]: the trait bound `usize: Trait` is not satisfied
37+
--> $DIR/on-structs-and-enums-static.rs:12:8
2038
|
2139
LL | x: 1,
2240
| ^ the trait `Trait` is not implemented for `usize`
@@ -32,6 +50,6 @@ note: required by a bound in `Foo`
3250
LL | struct Foo<T:Trait> {
3351
| ^^^^^ required by this bound in `Foo`
3452

35-
error: aborting due to 2 previous errors
53+
error: aborting due to 3 previous errors
3654

3755
For more information about this error, try `rustc --explain E0277`.

tests/ui/wf/wf-const-type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct NotCopy;
1010
const FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
1111
//~^ ERROR E0277
1212
//~| ERROR E0277
13+
//~| ERROR E0277
1314

1415

1516
fn main() { }

tests/ui/wf/wf-const-type.stderr

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ LL + #[derive(Copy)]
1616
LL | struct NotCopy;
1717
|
1818

19+
error[E0277]: the trait bound `NotCopy: Copy` is not satisfied
20+
--> $DIR/wf-const-type.rs:10:12
21+
|
22+
LL | const FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
23+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `NotCopy`, which is required by `Option<NotCopy>: Copy`
24+
|
25+
= note: required for `Option<NotCopy>` to implement `Copy`
26+
note: required by a bound in `IsCopy`
27+
--> $DIR/wf-const-type.rs:7:17
28+
|
29+
LL | struct IsCopy<T:Copy> { t: T }
30+
| ^^^^ required by this bound in `IsCopy`
31+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
32+
help: consider annotating `NotCopy` with `#[derive(Copy)]`
33+
|
34+
LL + #[derive(Copy)]
35+
LL | struct NotCopy;
36+
|
37+
1938
error[E0277]: the trait bound `NotCopy: Copy` is not satisfied
2039
--> $DIR/wf-const-type.rs:10:50
2140
|
@@ -34,6 +53,6 @@ LL + #[derive(Copy)]
3453
LL | struct NotCopy;
3554
|
3655

37-
error: aborting due to 2 previous errors
56+
error: aborting due to 3 previous errors
3857

3958
For more information about this error, try `rustc --explain E0277`.

tests/ui/wf/wf-static-type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct NotCopy;
1010
static FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
1111
//~^ ERROR E0277
1212
//~| ERROR E0277
13+
//~| ERROR E0277
1314

1415

1516
fn main() { }

tests/ui/wf/wf-static-type.stderr

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ LL + #[derive(Copy)]
1616
LL | struct NotCopy;
1717
|
1818

19+
error[E0277]: the trait bound `NotCopy: Copy` is not satisfied
20+
--> $DIR/wf-static-type.rs:10:13
21+
|
22+
LL | static FOO: IsCopy<Option<NotCopy>> = IsCopy { t: None };
23+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `NotCopy`, which is required by `Option<NotCopy>: Copy`
24+
|
25+
= note: required for `Option<NotCopy>` to implement `Copy`
26+
note: required by a bound in `IsCopy`
27+
--> $DIR/wf-static-type.rs:7:17
28+
|
29+
LL | struct IsCopy<T:Copy> { t: T }
30+
| ^^^^ required by this bound in `IsCopy`
31+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
32+
help: consider annotating `NotCopy` with `#[derive(Copy)]`
33+
|
34+
LL + #[derive(Copy)]
35+
LL | struct NotCopy;
36+
|
37+
1938
error[E0277]: the trait bound `NotCopy: Copy` is not satisfied
2039
--> $DIR/wf-static-type.rs:10:51
2140
|
@@ -34,6 +53,6 @@ LL + #[derive(Copy)]
3453
LL | struct NotCopy;
3554
|
3655

37-
error: aborting due to 2 previous errors
56+
error: aborting due to 3 previous errors
3857

3958
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)