Skip to content

Commit 44c6e83

Browse files
authored
Rollup merge of #134771 - compiler-errors:const-arg-has-type-err, r=lcnr
Report correct `SelectionError` for `ConstArgHasType` in new solver fulfill r? ``@BoxyUwU``
2 parents 49b05ed + 5922599 commit 44c6e83

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

compiler/rustc_trait_selection/src/solve/fulfill.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use rustc_infer::traits::{
1010
self, FromSolverError, MismatchedProjectionTypes, Obligation, ObligationCause,
1111
ObligationCauseCode, PredicateObligation, PredicateObligations, SelectionError, TraitEngine,
1212
};
13-
use rustc_middle::bug;
1413
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1514
use rustc_middle::ty::{self, TyCtxt};
15+
use rustc_middle::{bug, span_bug};
1616
use rustc_next_trait_solver::solve::{GenerateProofTree, HasChanged, SolverDelegateEvalExt as _};
1717
use tracing::{instrument, trace};
1818

@@ -258,6 +258,23 @@ fn fulfillment_error_for_no_solution<'tcx>(
258258
MismatchedProjectionTypes { err: TypeError::Mismatch },
259259
)
260260
}
261+
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, expected_ty)) => {
262+
let ct_ty = match ct.kind() {
263+
ty::ConstKind::Unevaluated(uv) => {
264+
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
265+
}
266+
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(obligation.param_env),
267+
_ => span_bug!(
268+
obligation.cause.span,
269+
"ConstArgHasWrongType failed but we don't know how to compute type"
270+
),
271+
};
272+
FulfillmentErrorCode::Select(SelectionError::ConstArgHasWrongType {
273+
ct,
274+
ct_ty,
275+
expected_ty,
276+
})
277+
}
261278
ty::PredicateKind::NormalizesTo(..) => {
262279
FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch })
263280
}

tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.stderr renamed to tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.current.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0308]: mismatched types
2-
--> $DIR/const-in-impl-fn-return-type.rs:15:39
2+
--> $DIR/const-in-impl-fn-return-type.rs:20:39
33
|
44
LL | fn func<const N: u32>() -> [(); { () }] {
55
| ^^ expected `usize`, found `()`
66

77
error: the constant `N` is not of type `usize`
8-
--> $DIR/const-in-impl-fn-return-type.rs:7:32
8+
--> $DIR/const-in-impl-fn-return-type.rs:12:32
99
|
1010
LL | fn func<const N: u32>() -> [(); N];
1111
| ^^^^^^^ expected `usize`, found `u32`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/const-in-impl-fn-return-type.rs:20:39
3+
|
4+
LL | fn func<const N: u32>() -> [(); { () }] {
5+
| ^^ expected `usize`, found `()`
6+
7+
error: the constant `N` is not of type `usize`
8+
--> $DIR/const-in-impl-fn-return-type.rs:12:32
9+
|
10+
LL | fn func<const N: u32>() -> [(); N];
11+
| ^^^^^^^ expected `usize`, found `u32`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0308`.

tests/ui/typeck/issue-114918/const-in-impl-fn-return-type.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
//@ revisions: current next
2+
//@[next] compile-flags: -Znext-solver
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
15
// Regression test for #114918
6+
27
// Test that a const generic enclosed in a block within the return type
38
// of an impl fn produces a type mismatch error instead of triggering
49
// a const eval cycle

0 commit comments

Comments
 (0)