Skip to content

Commit ba7d9f3

Browse files
committed
WIP
1 parent 544bf7b commit ba7d9f3

File tree

14 files changed

+69
-44
lines changed

14 files changed

+69
-44
lines changed

compiler/rustc_codegen_cranelift/src/global_asm.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
5353
Err(ErrorHandled::TooGeneric(_)) => {
5454
span_bug!(op_sp, "asm const cannot be resolved; too generic");
5555
}
56+
Err(ErrorHandled::ImpossiblePreds(_)) => {
57+
span_bug!(
58+
op_sp,
59+
"constant value failed to evaluate due to trivial bounds",
60+
);
61+
}
5662
}
5763
}
5864
InlineAsmOperand::SymFn { expr } => {

compiler/rustc_codegen_ssa/src/mono_item.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
6969
"asm const cannot be resolved; too generic"
7070
)
7171
}
72+
Err(ErrorHandled::ImpossiblePreds(_)) => {
73+
span_bug!(
74+
*op_sp,
75+
"asm const cannot be resolved; impossible preds"
76+
)
77+
}
7278
}
7379
}
7480
hir::InlineAsmOperand::SymFn { expr } => {

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
335335
tcx: TyCtxt<'tcx>,
336336
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
337337
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
338+
// Avoid evaluating instances with impossible bounds required to hold as
339+
// this can result in executing code that should never be executed.
340+
let instance_def = key.value.instance.def_id();
341+
if tcx.def_kind(instance_def) == DefKind::AnonConst
342+
&& let ty::AnonConstKind::GCEConst = tcx.anon_const_kind(instance_def)
343+
{ // ... do nothing for GCE anon consts as it would cycle
344+
} else {
345+
if tcx.instantiate_and_check_impossible_predicates((instance_def, key.value.instance.args))
346+
{
347+
return Err(ErrorHandled::ImpossiblePreds(tcx.def_span(instance_def)));
348+
}
349+
}
350+
338351
// This shouldn't be used for statics, since statics are conceptually places,
339352
// not values -- so what we do here could break pointer identity.
340353
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
544544
let const_val = val.eval(*ecx.tcx, ecx.typing_env, span).map_err(|err| {
545545
if M::ALL_CONSTS_ARE_PRECHECKED {
546546
match err {
547-
ErrorHandled::TooGeneric(..) => {},
547+
ErrorHandled::TooGeneric(..)
548+
| ErrorHandled::ImpossiblePreds(..) => {}
548549
ErrorHandled::Reported(reported, span) => {
549550
if reported.is_allowed_in_infallible() {
550551
// These errors can just sometimes happen, even when the expression

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@ fn check_type_defn<'tcx>(
12211221
Err(ErrorHandled::TooGeneric(sp)) => {
12221222
span_bug!(sp, "enum variant discr was too generic to eval")
12231223
}
1224+
Err(ErrorHandled::ImpossiblePreds(sp)) => {
1225+
_ = tcx
1226+
.dcx()
1227+
.span_err(sp, "constant value failed to evaluate due to trivial bounds")
1228+
}
12241229
}
12251230
}
12261231
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ provide! { tcx, def_id, other, cdata,
429429
doc_link_traits_in_scope => {
430430
tcx.arena.alloc_from_iter(cdata.get_doc_link_traits_in_scope(def_id.index))
431431
}
432+
anon_const_kind => { table }
432433
}
433434

434435
pub(in crate::rmeta) fn provide(providers: &mut Providers) {

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub enum ErrorHandled {
2525
/// Don't emit an error, the evaluation failed because the MIR was generic
2626
/// and the args didn't fully monomorphize it.
2727
TooGeneric(Span),
28+
ImpossiblePreds(Span),
2829
}
2930

3031
impl From<ReportedErrorInfo> for ErrorHandled {
@@ -39,6 +40,7 @@ impl ErrorHandled {
3940
match self {
4041
ErrorHandled::Reported(err, _span) => ErrorHandled::Reported(err, span),
4142
ErrorHandled::TooGeneric(_span) => ErrorHandled::TooGeneric(span),
43+
ErrorHandled::ImpossiblePreds(_span) => ErrorHandled::ImpossiblePreds(span),
4244
}
4345
}
4446

@@ -49,7 +51,7 @@ impl ErrorHandled {
4951
tcx.dcx().emit_note(error::ErroneousConstant { span });
5052
}
5153
}
52-
&ErrorHandled::TooGeneric(_) => {}
54+
ErrorHandled::TooGeneric(_) | ErrorHandled::ImpossiblePreds(_) => {}
5355
}
5456
}
5557
}
@@ -188,7 +190,9 @@ impl From<ErrorHandled> for InterpErrorInfo<'_> {
188190
fn from(err: ErrorHandled) -> Self {
189191
InterpErrorKind::InvalidProgram(match err {
190192
ErrorHandled::Reported(r, _span) => InvalidProgramInfo::AlreadyReported(r),
191-
ErrorHandled::TooGeneric(_span) => InvalidProgramInfo::TooGeneric,
193+
ErrorHandled::TooGeneric(_span) | ErrorHandled::ImpossiblePreds(_span) => {
194+
InvalidProgramInfo::TooGeneric
195+
}
192196
})
193197
.into()
194198
}

compiler/rustc_middle/src/ty/adt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ impl<'tcx> AdtDef<'tcx> {
525525
Err(err) => {
526526
let guar = match err {
527527
ErrorHandled::Reported(info, _) => info.into(),
528+
ErrorHandled::ImpossiblePreds(_span) => tcx.dcx().span_err(
529+
tcx.def_span(expr_did),
530+
"constant value failed to evaluate due to trivial bounds",
531+
),
528532
ErrorHandled::TooGeneric(..) => tcx.dcx().span_delayed_bug(
529533
tcx.def_span(expr_did),
530534
"enum discriminant depends on generics",

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'tcx> ConstToPat<'tcx> {
139139
}
140140
return self.mk_err(err, ty);
141141
}
142-
Err(ErrorHandled::TooGeneric(_)) => {
142+
Err(ErrorHandled::ImpossiblePreds(_) | ErrorHandled::TooGeneric(_)) => {
143143
let mut e = self
144144
.tcx
145145
.dcx()

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
661661
err.emit_note(self.tcx);
662662
return None;
663663
}
664+
Err(ErrorHandled::ImpossiblePreds(..)) => span_bug!(
665+
constant.span,
666+
"collection encountered constant with impossible preds: {:?}",
667+
const_,
668+
),
664669
}
665670
}
666671
}

0 commit comments

Comments
 (0)