Skip to content

Do not ICE on generic const expr referencing missing ty param #142810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,9 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
ty::ConstKind::Unevaluated(uv) => {
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
}
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(wfcx.param_env),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(wfcx.param_env).unwrap()
}
};

let param_ty = tcx.type_of(param.def_id).instantiate_identity();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl ParamConst {
}

#[instrument(level = "debug")]
pub fn find_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
pub fn find_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Option<Ty<'tcx>> {
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
// `ConstArgHasType` are never desugared to be higher ranked.
match clause.kind().skip_binder() {
Expand All @@ -362,9 +362,9 @@ impl ParamConst {
}
});

let ty = candidates.next().unwrap();
let ty = candidates.next()?;
assert!(candidates.next().is_none());
ty
Some(ty)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ pub(super) fn fulfillment_error_for_no_solution<'tcx>(
ty::ConstKind::Unevaluated(uv) => {
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
}
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(obligation.param_env),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(obligation.param_env).unwrap()
}
ty::ConstKind::Value(cv) => cv.ty,
kind => span_bug!(
obligation.cause.span,
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_infer::traits::{
use rustc_middle::bug;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, Ty, TypeVisitableExt, TypingMode};
use thin_vec::ThinVec;
use tracing::{debug, debug_span, instrument};

Expand Down Expand Up @@ -507,7 +507,16 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
}
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(obligation.param_env)
let Some(ty) = param_ct.find_ty_from_env(obligation.param_env) else {
return ProcessResult::Error(FulfillmentErrorCode::Select(
SelectionError::ConstArgHasWrongType {
ct,
ct_ty: Ty::new_misc_error(self.selcx.tcx()),
expected_ty: ty,
},
));
};
ty
}
};

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(obligation.param_env)
let Some(ty) = param_ct.find_ty_from_env(obligation.param_env) else {
return Ok(EvaluatedToErr);
};
ty
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs, adt_const_params)]
struct X<
const FN: () = {
|| {
let _: [(); B]; //~ ERROR cannot find value `B` in this scope
//~^ ERROR the constant `FN` is not of type `()`
};
},
>;
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0425]: cannot find value `B` in this scope
--> $DIR/missing_param_ty_while_evaluating_const_param.rs:6:25
|
LL | let _: [(); B];
| ^ not found in this scope

error: the constant `FN` is not of type `()`
--> $DIR/missing_param_ty_while_evaluating_const_param.rs:6:20
|
LL | let _: [(); B];
| ^^^^^^^ expected `()`, found type error
|
note: required by a const generic parameter in `X`
--> $DIR/missing_param_ty_while_evaluating_const_param.rs:4:5
|
LL | struct X<
| - required by a bound in this struct
LL | / const FN: () = {
LL | | || {
LL | | let _: [(); B];
... |
LL | | },
| |_____^ required by this const generic parameter in `X`

error: aborting due to 2 previous errors

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