Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,14 @@ where
expected_term: I::Term,
alias_const: ty::AliasConst<I>,
) -> QueryResultOrRerunNonErased<I> {
// Evaluation instantiates the constant's body with its arguments. An argument whose
// type does not match its parameter makes that body ill-formed, which CTFE cannot run.
self.try_evaluate_added_goals().inspect_err(|_| {
// Without recording the response here we attribute the error to this goal rather
// than to the nested goal which actually failed.
self.inspect.make_canonical_response(Certainty::Yes);
})?;

match self.evaluate_const(param_env, alias_const)? {
Some(evaluated) => {
self.eq(param_env, expected_term, evaluated.into())?;
Expand Down
55 changes: 55 additions & 0 deletions tests/ui/const-generics/gca/mismatched-const-arg-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/156780>.

//@ compile-flags: -Znext-solver

#![feature(
generic_const_args,
generic_const_items,
macroless_generic_const_args,
min_generic_const_args
)]
#![expect(incomplete_features)]

const F: f64 = 1.0;

const ADD1<const N: usize>: usize = N + 1;

const IGNORE<const N: usize>: usize = 0;

trait Tr {
const C<const N: usize>: usize;
}

impl Tr for () {
const C<const N: usize>: usize = N + 1;
}

fn main() {
let _: [(); ADD1::<1f64>];
//~^ ERROR: the constant `1f64` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<1u8>];
//~^ ERROR: the constant `1` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<1i32>];
//~^ ERROR: the constant `1` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<true>];
//~^ ERROR: the constant `true` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<'a'>];
//~^ ERROR: the constant `'a'` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<"s">];
//~^ ERROR: the constant `"s"` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); ADD1::<F>];
//~^ ERROR: the constant `1f64` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); IGNORE::<1f64>];
//~^ ERROR: the constant `1f64` is not of type `usize`
//~| ERROR: is not well-formed
let _: [(); <() as Tr>::C::<1f64>];
//~^ ERROR: type mismatch resolving `<() as Tr>::C<1f64> == _`
//~| ERROR: is not well-formed
}
111 changes: 111 additions & 0 deletions tests/ui/const-generics/gca/mismatched-const-arg-type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
error: the constant `1f64` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:28:12
|
LL | let _: [(); ADD1::<1f64>];
| ^^^^^^^^^^^^^^^^^^ expected `usize`, found `f64`

error: the constant `1` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:31:12
|
LL | let _: [(); ADD1::<1u8>];
| ^^^^^^^^^^^^^^^^^ expected `usize`, found `u8`

error: the constant `1` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:34:12
|
LL | let _: [(); ADD1::<1i32>];
| ^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`

error: the constant `true` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:37:12
|
LL | let _: [(); ADD1::<true>];
| ^^^^^^^^^^^^^^^^^^ expected `usize`, found `bool`

error: the constant `'a'` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:40:12
|
LL | let _: [(); ADD1::<'a'>];
| ^^^^^^^^^^^^^^^^^ expected `usize`, found `char`

error: the constant `"s"` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:43:12
|
LL | let _: [(); ADD1::<"s">];
| ^^^^^^^^^^^^^^^^^ expected `usize`, found `&'static str`

error: the constant `1f64` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:46:12
|
LL | let _: [(); ADD1::<F>];
| ^^^^^^^^^^^^^^^ expected `usize`, found `f64`

error: the constant `1f64` is not of type `usize`
--> $DIR/mismatched-const-arg-type.rs:49:12
|
LL | let _: [(); IGNORE::<1f64>];
| ^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `f64`

error[E0271]: type mismatch resolving `<() as Tr>::C<1f64> == _`
--> $DIR/mismatched-const-arg-type.rs:52:12
|
LL | let _: [(); <() as Tr>::C::<1f64>];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ

error: the type `[(); ADD1::<1f64>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:28:12
|
LL | let _: [(); ADD1::<1f64>];
| ^^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<1>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:31:12
|
LL | let _: [(); ADD1::<1u8>];
| ^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<1>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:34:12
|
LL | let _: [(); ADD1::<1i32>];
| ^^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<true>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:37:12
|
LL | let _: [(); ADD1::<true>];
| ^^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<'a'>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:40:12
|
LL | let _: [(); ADD1::<'a'>];
| ^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<"s">]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:43:12
|
LL | let _: [(); ADD1::<"s">];
| ^^^^^^^^^^^^^^^^^

error: the type `[(); ADD1::<1f64>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:46:12
|
LL | let _: [(); ADD1::<F>];
| ^^^^^^^^^^^^^^^

error: the type `[(); IGNORE::<1f64>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:49:12
|
LL | let _: [(); IGNORE::<1f64>];
| ^^^^^^^^^^^^^^^^^^^^

error: the type `[(); <() as Tr>::C::<1f64>]` is not well-formed
--> $DIR/mismatched-const-arg-type.rs:52:12
|
LL | let _: [(); <() as Tr>::C::<1f64>];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 18 previous errors

For more information about this error, try `rustc --explain E0271`.
Loading