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

Commit e852e36

Browse files
committed
Auto merge of rust-lang#128715 - Jaic1:master, r=<try>
Remove `try_from_lit` from `from_anon_const` This PR tries to fix rust-lang#116308. TODO: - [ ] write up the reason why `try_from_lit` should be removed from `from_anon_const` - [ ] write up the reason why it is safe to do this - [ ] do a perf run and see if doing this affect performance ui tests changes: - [ ] failed run-pass/check-pass (`const-arg-in-const-arg.rs#full`, `abstract-const-as-cast-4.rs`, `no_dependence.rs`, `issue-94293.rs`) - [ ] symbol mangling affected (`symbol-names/*`) - [ ] different error report (`const-projection-err.rs#gce`, `abstract-const-as-cast-3.rs`, `type_mismatch.rs`) - [x] misc - error report reordering - same error, but different const type reprs This PR is related to two unstable features (`adt_const_params`: rust-lang#95174, `generic_const_exprs`: rust-lang#76560). r? `@BoxyUwU`
2 parents 899eb03 + 4692924 commit e852e36

22 files changed

+187
-104
lines changed

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,13 @@ impl<'tcx> Const<'tcx> {
238238
let expr = &tcx.hir().body(body_id).value;
239239
debug!(?expr);
240240

241-
let ty = tcx.type_of(def).no_bound_vars().expect("const parameter types cannot be generic");
242-
243-
match Self::try_from_lit(tcx, ty, expr) {
244-
Some(v) => v,
245-
None => ty::Const::new_unevaluated(
246-
tcx,
247-
ty::UnevaluatedConst {
248-
def: def.to_def_id(),
249-
args: GenericArgs::identity_for_item(tcx, def.to_def_id()),
250-
},
251-
),
252-
}
241+
ty::Const::new_unevaluated(
242+
tcx,
243+
ty::UnevaluatedConst {
244+
def: def.to_def_id(),
245+
args: GenericArgs::identity_for_item(tcx, def.to_def_id()),
246+
},
247+
)
253248
}
254249

255250
/// Lower a const param to a [`Const`].

tests/ui/associated-type-bounds/const-projection-err.gce.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ LL | #![cfg_attr(gce, feature(generic_const_exprs))]
77
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
88
= note: `#[warn(incomplete_features)]` on by default
99

10-
error[E0271]: type mismatch resolving `<T as TraitWAssocConst>::A == 1`
10+
error[E0308]: mismatched types
1111
--> $DIR/const-projection-err.rs:14:11
1212
|
1313
LL | foo::<T>();
1414
| ^ expected `0`, found `1`
1515
|
16+
= note: expected constant `0`
17+
found constant `1`
1618
note: required by a bound in `foo`
1719
--> $DIR/const-projection-err.rs:11:28
1820
|
@@ -21,4 +23,4 @@ LL | fn foo<T: TraitWAssocConst<A = 1>>() {}
2123

2224
error: aborting due to 1 previous error; 1 warning emitted
2325

24-
For more information about this error, try `rustc --explain E0271`.
26+
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/defaults/mismatch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ pub struct Example4<const N: usize = 13, const M: usize = 4>;
66
fn main() {
77
let e: Example<13> = ();
88
//~^ Error: mismatched types
9-
//~| expected struct `Example`
9+
//~| expected struct `Example<13>`
1010
let e: Example2<u32, 13> = ();
1111
//~^ Error: mismatched types
12-
//~| expected struct `Example2`
12+
//~| expected struct `Example2<u32, 13>`
1313
let e: Example3<13, u32> = ();
1414
//~^ Error: mismatched types
15-
//~| expected struct `Example3`
15+
//~| expected struct `Example3<13>`
1616
let e: Example3<7> = ();
1717
//~^ Error: mismatched types
1818
//~| expected struct `Example3<7>`
1919
let e: Example4<7> = ();
2020
//~^ Error: mismatched types
21-
//~| expected struct `Example4<7>`
21+
//~| expected struct `Example4<7, 4>`
2222
}

tests/ui/const-generics/defaults/mismatch.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ error[E0308]: mismatched types
22
--> $DIR/mismatch.rs:7:26
33
|
44
LL | let e: Example<13> = ();
5-
| ----------- ^^ expected `Example`, found `()`
5+
| ----------- ^^ expected `Example<13>`, found `()`
66
| |
77
| expected due to this
88
|
9-
= note: expected struct `Example`
9+
= note: expected struct `Example<13>`
1010
found unit type `()`
1111

1212
error[E0308]: mismatched types
1313
--> $DIR/mismatch.rs:10:32
1414
|
1515
LL | let e: Example2<u32, 13> = ();
16-
| ----------------- ^^ expected `Example2`, found `()`
16+
| ----------------- ^^ expected `Example2<u32, 13>`, found `()`
1717
| |
1818
| expected due to this
1919
|
20-
= note: expected struct `Example2`
20+
= note: expected struct `Example2<u32, 13>`
2121
found unit type `()`
2222

2323
error[E0308]: mismatched types
2424
--> $DIR/mismatch.rs:13:32
2525
|
2626
LL | let e: Example3<13, u32> = ();
27-
| ----------------- ^^ expected `Example3`, found `()`
27+
| ----------------- ^^ expected `Example3<13>`, found `()`
2828
| |
2929
| expected due to this
3030
|
31-
= note: expected struct `Example3`
31+
= note: expected struct `Example3<13>`
3232
found unit type `()`
3333

3434
error[E0308]: mismatched types
@@ -46,11 +46,11 @@ error[E0308]: mismatched types
4646
--> $DIR/mismatch.rs:19:26
4747
|
4848
LL | let e: Example4<7> = ();
49-
| ----------- ^^ expected `Example4<7>`, found `()`
49+
| ----------- ^^ expected `Example4<7, 4>`, found `()`
5050
| |
5151
| expected due to this
5252
|
53-
= note: expected struct `Example4<7>`
53+
= note: expected struct `Example4<7, 4>`
5454
found unit type `()`
5555

5656
error: aborting due to 5 previous errors

tests/ui/const-generics/defaults/rp_impl_trait_fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn uwu<const N: u8>() -> impl Traitor<N> {
1919
}
2020

2121
fn owo() -> impl Traitor {
22-
//~^ error: the trait bound `u64: Traitor` is not satisfied
22+
//~^ error: the trait bound `u64: Traitor<1>` is not satisfied
2323
1_u64
2424
}
2525

tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ LL | 1_u32
2020
|
2121
= help: the trait `Traitor<N, 2>` is implemented for `u32`
2222

23-
error[E0277]: the trait bound `u64: Traitor` is not satisfied
23+
error[E0277]: the trait bound `u64: Traitor<1>` is not satisfied
2424
--> $DIR/rp_impl_trait_fail.rs:21:13
2525
|
2626
LL | fn owo() -> impl Traitor {
27-
| ^^^^^^^^^^^^ the trait `Traitor` is not implemented for `u64`
27+
| ^^^^^^^^^^^^ the trait `Traitor<1>` is not implemented for `u64`
2828
LL |
2929
LL | 1_u64
3030
| ----- return type was inferred to be `u64` here

tests/ui/const-generics/defaults/trait_objects_fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {
2424

2525
fn main() {
2626
foo(&10_u32);
27-
//~^ error: the trait bound `u32: Trait` is not satisfied
27+
//~^ error: the trait bound `u32: Trait<12>` is not satisfied
2828
bar(&true);
2929
//~^ error: the trait bound `bool: Traitor<_>` is not satisfied
3030
}

tests/ui/const-generics/defaults/trait_objects_fail.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `u32: Trait` is not satisfied
1+
error[E0277]: the trait bound `u32: Trait<12>` is not satisfied
22
--> $DIR/trait_objects_fail.rs:26:9
33
|
44
LL | foo(&10_u32);
5-
| ^^^^^^^ the trait `Trait` is not implemented for `u32`
5+
| ^^^^^^^ the trait `Trait<12>` is not implemented for `u32`
66
|
77
= help: the trait `Trait<2>` is implemented for `u32`
8-
= note: required for the cast from `&u32` to `&dyn Trait`
8+
= note: required for the cast from `&u32` to `&dyn Trait<12>`
99

1010
error[E0277]: the trait bound `bool: Traitor<_>` is not satisfied
1111
--> $DIR/trait_objects_fail.rs:28:9

tests/ui/const-generics/different_generic_args.full.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | u = ConstUsize::<4> {};
55
| ^^^^^^^^^^^^^^^^^^ expected `3`, found `4`
66
|
7-
= note: expected struct `ConstUsize<3>`
8-
found struct `ConstUsize<4>`
7+
= note: expected constant `3`
8+
found constant `4`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/const-generics/generic_const_exprs/abstract-const-as-cast-3.stderr

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ note: required by a bound in `use_trait_impl::assert_impl`
6868
LL | fn assert_impl<T: Trait>() {}
6969
| ^^^^^ required by this bound in `assert_impl`
7070

71+
error: unconstrained generic constant
72+
--> $DIR/abstract-const-as-cast-3.rs:23:19
73+
|
74+
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
|
77+
note: required for `HasCastInTraitImpl<13, { 12 as u128 }>` to implement `Trait`
78+
--> $DIR/abstract-const-as-cast-3.rs:8:22
79+
|
80+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
81+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
note: required by a bound in `use_trait_impl::assert_impl`
83+
--> $DIR/abstract-const-as-cast-3.rs:14:23
84+
|
85+
LL | fn assert_impl<T: Trait>() {}
86+
| ^^^^^ required by this bound in `assert_impl`
87+
help: try adding a `where` bound
88+
|
89+
LL | EvaluatableU128<{N as u128}>:, [(); { O as u128 } as usize]: {
90+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
7192
error[E0308]: mismatched types
7293
--> $DIR/abstract-const-as-cast-3.rs:23:5
7394
|
@@ -82,6 +103,27 @@ note: required by a bound in `use_trait_impl::assert_impl`
82103
LL | fn assert_impl<T: Trait>() {}
83104
| ^^^^^ required by this bound in `assert_impl`
84105

106+
error: unconstrained generic constant
107+
--> $DIR/abstract-const-as-cast-3.rs:25:19
108+
|
109+
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
111+
|
112+
note: required for `HasCastInTraitImpl<14, 13>` to implement `Trait`
113+
--> $DIR/abstract-const-as-cast-3.rs:8:22
114+
|
115+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
116+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117+
note: required by a bound in `use_trait_impl::assert_impl`
118+
--> $DIR/abstract-const-as-cast-3.rs:14:23
119+
|
120+
LL | fn assert_impl<T: Trait>() {}
121+
| ^^^^^ required by this bound in `assert_impl`
122+
help: try adding a `where` bound
123+
|
124+
LL | EvaluatableU128<{N as u128}>:, [(); { O as u128 } as usize]: {
125+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126+
85127
error[E0308]: mismatched types
86128
--> $DIR/abstract-const-as-cast-3.rs:25:5
87129
|
@@ -166,6 +208,27 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
166208
LL | fn assert_impl<T: Trait>() {}
167209
| ^^^^^ required by this bound in `assert_impl`
168210

211+
error: unconstrained generic constant
212+
--> $DIR/abstract-const-as-cast-3.rs:41:19
213+
|
214+
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
215+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216+
|
217+
note: required for `HasCastInTraitImpl<13, { 12 as u128 }>` to implement `Trait`
218+
--> $DIR/abstract-const-as-cast-3.rs:8:22
219+
|
220+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
221+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222+
note: required by a bound in `use_trait_impl_2::assert_impl`
223+
--> $DIR/abstract-const-as-cast-3.rs:32:23
224+
|
225+
LL | fn assert_impl<T: Trait>() {}
226+
| ^^^^^ required by this bound in `assert_impl`
227+
help: try adding a `where` bound
228+
|
229+
LL | EvaluatableU128<{N as _}>:, [(); { O as u128 } as usize]: {
230+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231+
169232
error[E0308]: mismatched types
170233
--> $DIR/abstract-const-as-cast-3.rs:41:5
171234
|
@@ -180,6 +243,27 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
180243
LL | fn assert_impl<T: Trait>() {}
181244
| ^^^^^ required by this bound in `assert_impl`
182245

246+
error: unconstrained generic constant
247+
--> $DIR/abstract-const-as-cast-3.rs:43:19
248+
|
249+
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
250+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
251+
|
252+
note: required for `HasCastInTraitImpl<14, 13>` to implement `Trait`
253+
--> $DIR/abstract-const-as-cast-3.rs:8:22
254+
|
255+
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
256+
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
257+
note: required by a bound in `use_trait_impl_2::assert_impl`
258+
--> $DIR/abstract-const-as-cast-3.rs:32:23
259+
|
260+
LL | fn assert_impl<T: Trait>() {}
261+
| ^^^^^ required by this bound in `assert_impl`
262+
help: try adding a `where` bound
263+
|
264+
LL | EvaluatableU128<{N as _}>:, [(); { O as u128 } as usize]: {
265+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266+
183267
error[E0308]: mismatched types
184268
--> $DIR/abstract-const-as-cast-3.rs:43:5
185269
|
@@ -194,6 +278,6 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
194278
LL | fn assert_impl<T: Trait>() {}
195279
| ^^^^^ required by this bound in `assert_impl`
196280

197-
error: aborting due to 12 previous errors
281+
error: aborting due to 16 previous errors
198282

199283
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)