Skip to content

Commit 89b0410

Browse files
committed
Don't probe InferConst in fold_const if self.infcx is None
1 parent 79734f1 commit 89b0410

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -456,17 +456,32 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
456456
fn fold_const(&mut self, mut ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
457457
match ct.kind() {
458458
ty::ConstKind::Infer(InferConst::Var(mut vid)) => {
459+
let Some(infcx) = self.infcx else {
460+
// FIXME(with_negative_coherence): the infcx has constraints from equating
461+
// the impl headers in `impl_intersection_has_negative_obligation`.
462+
// We should use these constraints as assumptions.
463+
assert!(self.tcx.features().with_negative_coherence);
464+
debug!("canonicalizing `ConstKind::Infer` without an `infcx`?");
465+
assert!(!self.canonicalize_mode.preserve_universes());
466+
return self.canonicalize_const_var(
467+
CanonicalVarInfo {
468+
kind: CanonicalVarKind::Const(ty::UniverseIndex::ROOT, ct.ty()),
469+
},
470+
ct,
471+
);
472+
};
473+
459474
// We need to canonicalize the *root* of our const var.
460475
// This is so that our canonical response correctly reflects
461476
// any equated inference vars correctly!
462-
let root_vid = self.infcx.unwrap().root_const_var(vid);
477+
let root_vid = infcx.root_const_var(vid);
463478
if root_vid != vid {
464479
ct = ty::Const::new_var(self.tcx, root_vid, ct.ty());
465480
vid = root_vid;
466481
}
467482

468483
debug!("canonical: const var found with vid {:?}", vid);
469-
match self.infcx.unwrap().probe_const_var(vid) {
484+
match infcx.probe_const_var(vid) {
470485
Ok(c) => {
471486
debug!("(resolved to {:?})", c);
472487
return self.fold_const(c);
@@ -487,7 +502,19 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
487502
}
488503
}
489504
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => {
490-
match self.infcx.unwrap().probe_effect_var(vid) {
505+
let Some(infcx) = self.infcx else {
506+
// FIXME(with_negative_coherence): the infcx has constraints from equating
507+
// the impl headers in `impl_intersection_has_negative_obligation`.
508+
// We should use these constraints as assumptions.
509+
assert!(self.tcx.features().with_negative_coherence);
510+
debug!("canonicalizing `ConstKind::Infer` without an `infcx`?");
511+
return self.canonicalize_const_var(
512+
CanonicalVarInfo { kind: CanonicalVarKind::Effect },
513+
ct,
514+
);
515+
};
516+
517+
match infcx.probe_effect_var(vid) {
491518
Some(value) => return self.fold_const(value),
492519
None => {
493520
return self.canonicalize_const_var(

src/tools/compiletest/src/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'test> TestCx<'test> {
381381
// if a test does not crash, consider it an error
382382
if proc_res.status.success() || matches!(proc_res.status.code(), Some(1 | 0)) {
383383
self.fatal(&format!(
384-
"test no longer crashes/triggers ICE! Please give it a mearningful name, \
384+
"test no longer crashes/triggers ICE! Please give it a meaningful name, \
385385
add a doc-comment to the start of the test explaining why it exists and \
386386
move it to tests/ui or wherever you see fit."
387387
));

tests/crashes/119381.rs

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Issue #119381: encountering `ty::ConstKind::Infer(InferConst::Var(_))` inside a `ParamEnv`
2+
3+
#![feature(with_negative_coherence)]
4+
trait Trait {}
5+
impl<const N: u8> Trait for [(); N] {}
6+
impl<const N: i8> Trait for [(); N] {}
7+
//~^ conflicting implementations of trait `Trait` for type `[(); _]`
8+
//~| mismatched types
9+
//~^^^^ mismatched types
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0119]: conflicting implementations of trait `Trait` for type `[(); _]`
2+
--> $DIR/infer-const-in-param-env.rs:6:1
3+
|
4+
LL | impl<const N: u8> Trait for [(); N] {}
5+
| ----------------------------------- first implementation here
6+
LL | impl<const N: i8> Trait for [(); N] {}
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `[(); _]`
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/infer-const-in-param-env.rs:5:34
11+
|
12+
LL | impl<const N: u8> Trait for [(); N] {}
13+
| ^ expected `usize`, found `u8`
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/infer-const-in-param-env.rs:6:34
17+
|
18+
LL | impl<const N: i8> Trait for [(); N] {}
19+
| ^ expected `usize`, found `i8`
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors have detailed explanations: E0119, E0308.
24+
For more information about an error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)