Skip to content

Commit d25c2e4

Browse files
committed
normalize obligations when checking ConstArgHasType hack
1 parent c48caa2 commit d25c2e4

6 files changed

Lines changed: 72 additions & 74 deletions

File tree

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,13 +958,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
958958
// type as a side effect of the anon const desugaring. To make this "consistent"
959959
// for users we explicitly check `ConstArgHasType` clauses so that const args
960960
// that don't go through an anon const still have their types checked.
961-
//
962-
// We use the unnormalized type as this mirrors the behaviour that we previously
963-
// would have had when all const arguments were anon consts.
964-
if let Some(unnormalized_obligations) = wfcx.unnormalized_obligations(span, ty)
961+
if let Some(obligations) = wfcx.obligations(span, ty)
965962
{
966963
let filtered_obligations =
967-
unnormalized_obligations.into_iter().filter(|o| {
964+
obligations.into_iter().filter(|o| {
968965
matches!(o.predicate.kind().skip_binder(),
969966
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, _))
970967
if matches!(ct.kind(), ty::ConstKind::Param(..)))

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,18 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
125125
));
126126
}
127127

128-
pub(super) fn unnormalized_obligations(
128+
pub(super) fn obligations(
129129
&self,
130130
span: Span,
131131
ty: Ty<'tcx>,
132132
) -> Option<PredicateObligations<'tcx>> {
133-
traits::wf::unnormalized_obligations(
133+
traits::wf::obligations(
134134
self.ocx.infcx,
135135
self.param_env,
136+
self.body_def_id,
137+
0,
136138
ty.into(),
137139
span,
138-
self.body_def_id,
139140
)
140141
}
141142
}

tests/ui/const-generics/check_const_arg_type_in_free_alias.eager.stderr

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ LL | type ArrLen<const B: bool> = [(); B];
66
|
77
= note: the length of array `[(); B]` must be type `usize`
88

9+
error[E0308]: mismatched types
10+
--> $DIR/check_const_arg_type_in_free_alias.rs:17:24
11+
|
12+
LL | type AnonArrLen = [(); true];
13+
| ^^^^ expected `usize`, found `bool`
14+
915
error: the constant `B` is not of type `usize`
1016
--> $DIR/check_const_arg_type_in_free_alias.rs:19:1
1117
|
@@ -18,6 +24,12 @@ note: required by a const generic parameter in `Foo`
1824
LL | struct Foo<const N: usize>;
1925
| ^^^^^^^^^^^^^^ required by this const generic parameter in `Foo`
2026

27+
error[E0308]: mismatched types
28+
--> $DIR/check_const_arg_type_in_free_alias.rs:21:25
29+
|
30+
LL | type AnonConstArg = Foo<true>;
31+
| ^^^^ expected `usize`, found `bool`
32+
2133
error: the constant `B` is not of type `usize`
2234
--> $DIR/check_const_arg_type_in_free_alias.rs:32:1
2335
|
@@ -30,6 +42,12 @@ note: required by a const generic parameter in `IdentityWithUnused`
3042
LL | trait IdentityWithUnused<const N: usize> {
3143
| ^^^^^^^^^^^^^^ required by this const generic parameter in `IdentityWithUnused`
3244

45+
error[E0308]: mismatched types
46+
--> $DIR/check_const_arg_type_in_free_alias.rs:34:44
47+
|
48+
LL | type AnonAlias = <() as IdentityWithUnused<true>>::This;
49+
| ^^^^ expected `usize`, found `bool`
50+
3351
error: the constant `B` is not of type `usize`
3452
--> $DIR/check_const_arg_type_in_free_alias.rs:38:1
3553
|
@@ -38,6 +56,18 @@ LL | type UseFree<const B: bool> = Free<B>;
3856
|
3957
= note: the length of array `[(); B]` must be type `usize`
4058

59+
error[E0308]: mismatched types
60+
--> $DIR/check_const_arg_type_in_free_alias.rs:40:25
61+
|
62+
LL | type AnonUseFree = Free<true>;
63+
| ^^^^ expected `usize`, found `bool`
64+
65+
error[E0308]: mismatched types
66+
--> $DIR/check_const_arg_type_in_free_alias.rs:53:45
67+
|
68+
LL | type AnonUseFreeIndirectlyCorrect = UseFree<1_usize>;
69+
| ^^^^^^^ expected `bool`, found `usize`
70+
4171
error: the constant `B` is not of type `usize`
4272
--> $DIR/check_const_arg_type_in_free_alias.rs:58:1
4373
|
@@ -46,6 +76,12 @@ LL | type IndirectArr<const B: bool> = Wrap<Wrap<[(); B]>>;
4676
|
4777
= note: the length of array `[(); B]` must be type `usize`
4878

79+
error[E0308]: mismatched types
80+
--> $DIR/check_const_arg_type_in_free_alias.rs:60:39
81+
|
82+
LL | type AnonIndirectArr = Wrap<Wrap<[(); true]>>;
83+
| ^^^^ expected `usize`, found `bool`
84+
4985
error: the constant `B` is not of type `usize`
5086
--> $DIR/check_const_arg_type_in_free_alias.rs:62:1
5187
|
@@ -58,42 +94,6 @@ note: required by a const generic parameter in `Foo`
5894
LL | struct Foo<const N: usize>;
5995
| ^^^^^^^^^^^^^^ required by this const generic parameter in `Foo`
6096

61-
error[E0308]: mismatched types
62-
--> $DIR/check_const_arg_type_in_free_alias.rs:17:24
63-
|
64-
LL | type AnonArrLen = [(); true];
65-
| ^^^^ expected `usize`, found `bool`
66-
67-
error[E0308]: mismatched types
68-
--> $DIR/check_const_arg_type_in_free_alias.rs:21:25
69-
|
70-
LL | type AnonConstArg = Foo<true>;
71-
| ^^^^ expected `usize`, found `bool`
72-
73-
error[E0308]: mismatched types
74-
--> $DIR/check_const_arg_type_in_free_alias.rs:34:44
75-
|
76-
LL | type AnonAlias = <() as IdentityWithUnused<true>>::This;
77-
| ^^^^ expected `usize`, found `bool`
78-
79-
error[E0308]: mismatched types
80-
--> $DIR/check_const_arg_type_in_free_alias.rs:40:25
81-
|
82-
LL | type AnonUseFree = Free<true>;
83-
| ^^^^ expected `usize`, found `bool`
84-
85-
error[E0308]: mismatched types
86-
--> $DIR/check_const_arg_type_in_free_alias.rs:53:45
87-
|
88-
LL | type AnonUseFreeIndirectlyCorrect = UseFree<1_usize>;
89-
| ^^^^^^^ expected `bool`, found `usize`
90-
91-
error[E0308]: mismatched types
92-
--> $DIR/check_const_arg_type_in_free_alias.rs:60:39
93-
|
94-
LL | type AnonIndirectArr = Wrap<Wrap<[(); true]>>;
95-
| ^^^^ expected `usize`, found `bool`
96-
9797
error[E0308]: mismatched types
9898
--> $DIR/check_const_arg_type_in_free_alias.rs:64:43
9999
|

tests/ui/const-generics/mgca/explicit_anon_consts.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ error: complex const arguments must be placed inside of a `const` block
3434
LL | struct Default4<const N: usize, const M: usize = { 1 + 1 }>;
3535
| ^^^^^^^^^
3636

37+
error: generic parameters may not be used in const operations
38+
--> $DIR/explicit_anon_consts.rs:11:41
39+
|
40+
LL | type Adt3<const N: usize> = Foo<const { N }>;
41+
| ^
42+
|
43+
= help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items
44+
45+
error: generic parameters may not be used in const operations
46+
--> $DIR/explicit_anon_consts.rs:19:42
47+
|
48+
LL | type Arr3<const N: usize> = [(); const { N }];
49+
| ^
50+
|
51+
= help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items
52+
3753
error: generic parameters may not be used in const operations
3854
--> $DIR/explicit_anon_consts.rs:42:51
3955
|
@@ -74,21 +90,5 @@ LL | let _6: [(); const { N }] = todo!();
7490
|
7591
= help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items
7692

77-
error: generic parameters may not be used in const operations
78-
--> $DIR/explicit_anon_consts.rs:11:41
79-
|
80-
LL | type Adt3<const N: usize> = Foo<const { N }>;
81-
| ^
82-
|
83-
= help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items
84-
85-
error: generic parameters may not be used in const operations
86-
--> $DIR/explicit_anon_consts.rs:19:42
87-
|
88-
LL | type Arr3<const N: usize> = [(); const { N }];
89-
| ^
90-
|
91-
= help: add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items
92-
9393
error: aborting due to 13 previous errors
9494

tests/ui/type/pattern_types/range_patterns.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0080]: evaluation panicked: exclusive range end at minimum value of type
2+
--> $DIR/range_patterns.rs:25:37
3+
|
4+
LL | type WRAP = pattern_type!(u32 is 1..0);
5+
| ^ evaluation of `WRAP::{constant#1}` failed here
6+
17
error: layout_of(NonZero<u32>) = Layout {
28
size: Size(4 bytes),
39
align: AbiAlign {
@@ -363,12 +369,6 @@ error: the type has an unknown layout
363369
LL | type EMPTY = pattern_type!(u32 is 1..1);
364370
| ^^^^^^^^^^
365371

366-
error[E0080]: evaluation panicked: exclusive range end at minimum value of type
367-
--> $DIR/range_patterns.rs:25:37
368-
|
369-
LL | type WRAP = pattern_type!(u32 is 1..0);
370-
| ^ evaluation of `WRAP::{constant#1}` failed here
371-
372372
error: the type has an unknown layout
373373
--> $DIR/range_patterns.rs:25:1
374374
|

tests/ui/type/pattern_types/signed_ranges.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
error[E0600]: cannot apply unary operator `-` to type `u32`
2+
--> $DIR/signed_ranges.rs:6:34
3+
|
4+
LL | type Sign = pattern_type!(u32 is -10..);
5+
| ^^^ cannot apply unary operator `-`
6+
|
7+
= note: unsigned values cannot be negated
8+
19
error[E0277]: the trait bound `u8: Neg` is not satisfied
210
--> $DIR/signed_ranges.rs:14:9
311
|
@@ -21,14 +29,6 @@ error[E0277]: the trait bound `char: Neg` is not satisfied
2129
LL | -'\0'..'a' => {}
2230
| ^^^^^ the trait `Neg` is not implemented for `char`
2331

24-
error[E0600]: cannot apply unary operator `-` to type `u32`
25-
--> $DIR/signed_ranges.rs:6:34
26-
|
27-
LL | type Sign = pattern_type!(u32 is -10..);
28-
| ^^^ cannot apply unary operator `-`
29-
|
30-
= note: unsigned values cannot be negated
31-
3232
error[E0600]: cannot apply unary operator `-` to type `char`
3333
--> $DIR/signed_ranges.rs:9:41
3434
|

0 commit comments

Comments
 (0)