Skip to content

Commit 4183c2d

Browse files
committed
Don't canonicalize ty::Const if its ParamEnv isn't canonicalized yet
1 parent 5b1d58c commit 4183c2d

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

compiler/rustc_infer/src/infer/relate/combine.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,18 @@ impl<'tcx> InferCtxt<'tcx> {
170170
return;
171171
}
172172

173+
let param_env = relation.param_env();
174+
if !self.tcx.canonical_param_env_cache.contains_key(param_env) {
175+
// Canonicalizing `param_env` may ICE as `CanonicalParamEnvCache::get_or_insert` requires that `InferCtxt`
176+
// isn't used, see #119381.
177+
return;
178+
}
179+
173180
// We don't have access to trait solving machinery in `rustc_infer` so the logic for determining if the
174181
// two const param's types are able to be equal has to go through a canonical query with the actual logic
175182
// in `rustc_trait_selection`.
176183
let canonical = self.canonicalize_query(
177-
relation.param_env().and((a.ty(), b.ty())),
184+
param_env.and((a.ty(), b.ty())),
178185
&mut OriginalQueryValues::default(),
179186
);
180187
self.tcx.check_tys_might_be_eq(canonical).unwrap_or_else(|_| {

compiler/rustc_middle/src/infer/canonical.rs

+4
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,8 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
356356
}
357357
}
358358
}
359+
360+
pub fn contains_key(&self, key: ty::ParamEnv<'tcx>) -> bool {
361+
self.map.borrow().contains_key(&key)
362+
}
359363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Issue #119381: `InferCtxt::super_combine_consts` shouldn't canonicalize its `ty::Const` if its
2+
//! `ParamEnv` isn't canonicalized yet.
3+
4+
#![feature(with_negative_coherence)]
5+
6+
impl<const N: u8> Copy for [(); N] {}
7+
//~^ only traits defined in the current crate can be implemented for arbitrary types
8+
//~| mismatched types
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
2+
--> $DIR/combine-consts-with-negative-coherence.rs:6:1
3+
|
4+
LL | impl<const N: u8> Copy for [(); N] {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-------
6+
| | |
7+
| | this is not defined in the current crate because arrays are always foreign
8+
| impl doesn't use only types from inside the current crate
9+
|
10+
= note: define and implement a trait or new type instead
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/combine-consts-with-negative-coherence.rs:6:33
14+
|
15+
LL | impl<const N: u8> Copy for [(); N] {}
16+
| ^ expected `usize`, found `u8`
17+
18+
error: aborting due to 2 previous errors
19+
20+
Some errors have detailed explanations: E0117, E0308.
21+
For more information about an error, try `rustc --explain E0117`.

0 commit comments

Comments
 (0)