Skip to content

Commit 00f369a

Browse files
Add note to find_const_ty_from_env
1 parent 9c4ff56 commit 00f369a

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,9 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
15571557
ty::ConstKind::Unevaluated(uv) => {
15581558
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
15591559
}
1560-
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(wfcx.param_env),
1560+
ty::ConstKind::Param(param_ct) => {
1561+
param_ct.find_const_ty_from_env(wfcx.param_env)
1562+
}
15611563
};
15621564

15631565
let param_ty = tcx.type_of(param.def_id).instantiate_identity();

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,19 @@ impl Placeholder<BoundVar> {
926926
}
927927
});
928928

929-
let ty = candidates.next().unwrap();
930-
assert!(candidates.next().is_none());
929+
// N.B. it may be tempting to fix ICEs by making this function return
930+
// `Option<Ty<'tcx>>` instead of `Ty<'tcx>`; however, this is generally
931+
// considered to be a bandaid solution, since it hides more important
932+
// underlying issues with how we construct generics and predicates of
933+
// items. It's advised to fix the underlying issue rather than trying
934+
// to modify this function.
935+
let ty = candidates.next().unwrap_or_else(|| {
936+
bug!("cannot find `{self:?}` in param-env: {env:#?}");
937+
});
938+
assert!(
939+
candidates.next().is_none(),
940+
"did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
941+
);assert!(candidates.next().is_none());
931942
ty
932943
}
933944
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl ParamConst {
346346
}
347347

348348
#[instrument(level = "debug")]
349-
pub fn find_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
349+
pub fn find_const_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
350350
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
351351
// `ConstArgHasType` are never desugared to be higher ranked.
352352
match clause.kind().skip_binder() {
@@ -362,8 +362,19 @@ impl ParamConst {
362362
}
363363
});
364364

365-
let ty = candidates.next().unwrap();
366-
assert!(candidates.next().is_none());
365+
// N.B. it may be tempting to fix ICEs by making this function return
366+
// `Option<Ty<'tcx>>` instead of `Ty<'tcx>`; however, this is generally
367+
// considered to be a bandaid solution, since it hides more important
368+
// underlying issues with how we construct generics and predicates of
369+
// items. It's advised to fix the underlying issue rather than trying
370+
// to modify this function.
371+
let ty = candidates.next().unwrap_or_else(|| {
372+
bug!("cannot find `{self:?}` in param-env: {env:#?}");
373+
});
374+
assert!(
375+
candidates.next().is_none(),
376+
"did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
377+
);
367378
ty
368379
}
369380
}

compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ pub(super) fn fulfillment_error_for_no_solution<'tcx>(
3939
ty::ConstKind::Unevaluated(uv) => {
4040
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
4141
}
42-
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(obligation.param_env),
42+
ty::ConstKind::Param(param_ct) => {
43+
param_ct.find_const_ty_from_env(obligation.param_env)
44+
}
4345
ty::ConstKind::Value(cv) => cv.ty,
4446
kind => span_bug!(
4547
obligation.cause.span,

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
507507
}
508508
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
509509
ty::ConstKind::Param(param_ct) => {
510-
param_ct.find_ty_from_env(obligation.param_env)
510+
param_ct.find_const_ty_from_env(obligation.param_env)
511511
}
512512
};
513513

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
979979
}
980980
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
981981
ty::ConstKind::Param(param_ct) => {
982-
param_ct.find_ty_from_env(obligation.param_env)
982+
param_ct.find_const_ty_from_env(obligation.param_env)
983983
}
984984
};
985985

0 commit comments

Comments
 (0)