Skip to content

Commit

Permalink
Auto merge of rust-lang#128715 - Jaic1:master, r=<try>
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Aug 9, 2024
2 parents 899eb03 + 4692924 commit e852e36
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 104 deletions.
19 changes: 7 additions & 12 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,13 @@ impl<'tcx> Const<'tcx> {
let expr = &tcx.hir().body(body_id).value;
debug!(?expr);

let ty = tcx.type_of(def).no_bound_vars().expect("const parameter types cannot be generic");

match Self::try_from_lit(tcx, ty, expr) {
Some(v) => v,
None => ty::Const::new_unevaluated(
tcx,
ty::UnevaluatedConst {
def: def.to_def_id(),
args: GenericArgs::identity_for_item(tcx, def.to_def_id()),
},
),
}
ty::Const::new_unevaluated(
tcx,
ty::UnevaluatedConst {
def: def.to_def_id(),
args: GenericArgs::identity_for_item(tcx, def.to_def_id()),
},
)
}

/// Lower a const param to a [`Const`].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ LL | #![cfg_attr(gce, feature(generic_const_exprs))]
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
= note: `#[warn(incomplete_features)]` on by default

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

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

For more information about this error, try `rustc --explain E0271`.
For more information about this error, try `rustc --explain E0308`.
8 changes: 4 additions & 4 deletions tests/ui/const-generics/defaults/mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ pub struct Example4<const N: usize = 13, const M: usize = 4>;
fn main() {
let e: Example<13> = ();
//~^ Error: mismatched types
//~| expected struct `Example`
//~| expected struct `Example<13>`
let e: Example2<u32, 13> = ();
//~^ Error: mismatched types
//~| expected struct `Example2`
//~| expected struct `Example2<u32, 13>`
let e: Example3<13, u32> = ();
//~^ Error: mismatched types
//~| expected struct `Example3`
//~| expected struct `Example3<13>`
let e: Example3<7> = ();
//~^ Error: mismatched types
//~| expected struct `Example3<7>`
let e: Example4<7> = ();
//~^ Error: mismatched types
//~| expected struct `Example4<7>`
//~| expected struct `Example4<7, 4>`
}
16 changes: 8 additions & 8 deletions tests/ui/const-generics/defaults/mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ error[E0308]: mismatched types
--> $DIR/mismatch.rs:7:26
|
LL | let e: Example<13> = ();
| ----------- ^^ expected `Example`, found `()`
| ----------- ^^ expected `Example<13>`, found `()`
| |
| expected due to this
|
= note: expected struct `Example`
= note: expected struct `Example<13>`
found unit type `()`

error[E0308]: mismatched types
--> $DIR/mismatch.rs:10:32
|
LL | let e: Example2<u32, 13> = ();
| ----------------- ^^ expected `Example2`, found `()`
| ----------------- ^^ expected `Example2<u32, 13>`, found `()`
| |
| expected due to this
|
= note: expected struct `Example2`
= note: expected struct `Example2<u32, 13>`
found unit type `()`

error[E0308]: mismatched types
--> $DIR/mismatch.rs:13:32
|
LL | let e: Example3<13, u32> = ();
| ----------------- ^^ expected `Example3`, found `()`
| ----------------- ^^ expected `Example3<13>`, found `()`
| |
| expected due to this
|
= note: expected struct `Example3`
= note: expected struct `Example3<13>`
found unit type `()`

error[E0308]: mismatched types
Expand All @@ -46,11 +46,11 @@ error[E0308]: mismatched types
--> $DIR/mismatch.rs:19:26
|
LL | let e: Example4<7> = ();
| ----------- ^^ expected `Example4<7>`, found `()`
| ----------- ^^ expected `Example4<7, 4>`, found `()`
| |
| expected due to this
|
= note: expected struct `Example4<7>`
= note: expected struct `Example4<7, 4>`
found unit type `()`

error: aborting due to 5 previous errors
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/defaults/rp_impl_trait_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn uwu<const N: u8>() -> impl Traitor<N> {
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ LL | 1_u32
|
= help: the trait `Traitor<N, 2>` is implemented for `u32`

error[E0277]: the trait bound `u64: Traitor` is not satisfied
error[E0277]: the trait bound `u64: Traitor<1>` is not satisfied
--> $DIR/rp_impl_trait_fail.rs:21:13
|
LL | fn owo() -> impl Traitor {
| ^^^^^^^^^^^^ the trait `Traitor` is not implemented for `u64`
| ^^^^^^^^^^^^ the trait `Traitor<1>` is not implemented for `u64`
LL |
LL | 1_u64
| ----- return type was inferred to be `u64` here
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/defaults/trait_objects_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn bar<const N: u8>(arg: &dyn Traitor<N>) -> u8 {

fn main() {
foo(&10_u32);
//~^ error: the trait bound `u32: Trait` is not satisfied
//~^ error: the trait bound `u32: Trait<12>` is not satisfied
bar(&true);
//~^ error: the trait bound `bool: Traitor<_>` is not satisfied
}
6 changes: 3 additions & 3 deletions tests/ui/const-generics/defaults/trait_objects_fail.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0277]: the trait bound `u32: Trait` is not satisfied
error[E0277]: the trait bound `u32: Trait<12>` is not satisfied
--> $DIR/trait_objects_fail.rs:26:9
|
LL | foo(&10_u32);
| ^^^^^^^ the trait `Trait` is not implemented for `u32`
| ^^^^^^^ the trait `Trait<12>` is not implemented for `u32`
|
= help: the trait `Trait<2>` is implemented for `u32`
= note: required for the cast from `&u32` to `&dyn Trait`
= note: required for the cast from `&u32` to `&dyn Trait<12>`

error[E0277]: the trait bound `bool: Traitor<_>` is not satisfied
--> $DIR/trait_objects_fail.rs:28:9
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/different_generic_args.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | u = ConstUsize::<4> {};
| ^^^^^^^^^^^^^^^^^^ expected `3`, found `4`
|
= note: expected struct `ConstUsize<3>`
found struct `ConstUsize<4>`
= note: expected constant `3`
found constant `4`

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ note: required by a bound in `use_trait_impl::assert_impl`
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:23:19
|
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: required for `HasCastInTraitImpl<13, { 12 as u128 }>` to implement `Trait`
--> $DIR/abstract-const-as-cast-3.rs:8:22
|
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`
help: try adding a `where` bound
|
LL | EvaluatableU128<{N as u128}>:, [(); { O as u128 } as usize]: {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:23:5
|
Expand All @@ -82,6 +103,27 @@ note: required by a bound in `use_trait_impl::assert_impl`
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:25:19
|
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: required for `HasCastInTraitImpl<14, 13>` to implement `Trait`
--> $DIR/abstract-const-as-cast-3.rs:8:22
|
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`
help: try adding a `where` bound
|
LL | EvaluatableU128<{N as u128}>:, [(); { O as u128 } as usize]: {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:25:5
|
Expand Down Expand Up @@ -166,6 +208,27 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:41:19
|
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: required for `HasCastInTraitImpl<13, { 12 as u128 }>` to implement `Trait`
--> $DIR/abstract-const-as-cast-3.rs:8:22
|
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`
help: try adding a `where` bound
|
LL | EvaluatableU128<{N as _}>:, [(); { O as u128 } as usize]: {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:41:5
|
Expand All @@ -180,6 +243,27 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:43:19
|
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: required for `HasCastInTraitImpl<14, 13>` to implement `Trait`
--> $DIR/abstract-const-as-cast-3.rs:8:22
|
LL | impl<const O: usize> Trait for HasCastInTraitImpl<O, { O as u128 }> {}
| ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`
help: try adding a `where` bound
|
LL | EvaluatableU128<{N as _}>:, [(); { O as u128 } as usize]: {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:43:5
|
Expand All @@ -194,6 +278,6 @@ note: required by a bound in `use_trait_impl_2::assert_impl`
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `assert_impl`

error: aborting due to 12 previous errors
error: aborting due to 16 previous errors

For more information about this error, try `rustc --explain E0308`.
36 changes: 19 additions & 17 deletions tests/ui/const-generics/generic_const_exprs/type_mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ LL | const ASSOC: usize;
LL | impl<const N: u64> Q for [u8; N] {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `ASSOC` in implementation

error: the constant `13` is not of type `u64`
--> $DIR/type_mismatch.rs:12:26
error[E0391]: cycle detected when building an abstract representation for `q_user::{constant#0}`
--> $DIR/type_mismatch.rs:12:25
|
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
| ^^^^^^^^ expected `u64`, found `usize`
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: required for `[u8; 13]` to implement `Q`
--> $DIR/type_mismatch.rs:8:20
note: ...which requires building THIR for `q_user::{constant#0}`...
--> $DIR/type_mismatch.rs:12:25
|
LL | impl<const N: u64> Q for [u8; N] {}
| ------------ ^ ^^^^^^^
| |
| unsatisfied trait bound introduced here

error[E0308]: mismatched types
--> $DIR/type_mismatch.rs:12:20
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
| ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `q_user::{constant#0}`...
--> $DIR/type_mismatch.rs:12:25
|
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
| ^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires building an abstract representation for `q_user::{constant#0}`, completing the cycle
note: cycle used when checking that `q_user` is well-formed
--> $DIR/type_mismatch.rs:12:1
|
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
| ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `[u8; <[u8; 13] as Q>::ASSOC]`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0046, E0308.
Some errors have detailed explanations: E0046, E0391.
For more information about an error, try `rustc --explain E0046`.
22 changes: 11 additions & 11 deletions tests/ui/const-generics/issues/issue-90318.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
= help: consider moving this anonymous constant into a `const` function
= note: this operation may be supported in the future

error: overly complex generic constant
--> $DIR/issue-90318.rs:22:8
|
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
| |
| borrowing is not supported in generic constants
|
= help: consider moving this anonymous constant into a `const` function
= note: this operation may be supported in the future

error[E0015]: cannot call non-const operator in constants
--> $DIR/issue-90318.rs:14:10
|
Expand All @@ -34,6 +23,17 @@ help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
LL + #![feature(const_trait_impl)]
|

error: overly complex generic constant
--> $DIR/issue-90318.rs:22:8
|
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
| |
| borrowing is not supported in generic constants
|
= help: consider moving this anonymous constant into a `const` function
= note: this operation may be supported in the future

error[E0015]: cannot call non-const operator in constants
--> $DIR/issue-90318.rs:22:10
|
Expand Down
Loading

0 comments on commit e852e36

Please sign in to comment.