Skip to content

Avoid ICE when Canonicalizer.infcx is None #119416

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

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 9 additions & 9 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,19 +475,19 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
}

fn fold_const(&mut self, mut ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Infer(InferConst::Var(mut vid)) => {
match (ct.kind(), self.infcx) {
(ty::ConstKind::Infer(InferConst::Var(mut vid)), Some(infcx)) => {
// We need to canonicalize the *root* of our const var.
// This is so that our canonical response correctly reflects
// any equated inference vars correctly!
let root_vid = self.infcx.unwrap().root_const_var(vid);
let root_vid = infcx.root_const_var(vid);
if root_vid != vid {
ct = ty::Const::new_var(self.tcx, root_vid, ct.ty());
vid = root_vid;
}

debug!("canonical: const var found with vid {:?}", vid);
match self.infcx.unwrap().probe_const_var(vid) {
match infcx.probe_const_var(vid) {
Ok(c) => {
debug!("(resolved to {:?})", c);
return self.fold_const(c);
Expand All @@ -507,8 +507,8 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
}
}
}
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => {
match self.infcx.unwrap().probe_effect_var(vid) {
(ty::ConstKind::Infer(InferConst::EffectVar(vid)), Some(infcx)) => {
match infcx.probe_effect_var(vid) {
Some(value) => return self.fold_const(value.as_const(self.tcx)),
None => {
return self.canonicalize_const_var(
Expand All @@ -518,17 +518,17 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
}
}
}
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
(ty::ConstKind::Infer(InferConst::Fresh(_)), _) => {
bug!("encountered a fresh const during canonicalization")
}
ty::ConstKind::Bound(debruijn, _) => {
(ty::ConstKind::Bound(debruijn, _), _) => {
if debruijn >= self.binder_index {
bug!("escaping bound const during canonicalization")
} else {
return ct;
}
}
ty::ConstKind::Placeholder(placeholder) => {
(ty::ConstKind::Placeholder(placeholder), _) => {
return self.canonicalize_const_var(
CanonicalVarInfo {
kind: CanonicalVarKind::PlaceholderConst(placeholder, ct.ty()),
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/coherence/with-negative-coherence-issue-119381.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(with_negative_coherence)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title of this issue doesn't really explain why it's an ICE. Specifically, there's no explanation for why we're encountering an inference variable in the type of a constant inside of a param-env, which is why this ICE occurs.


#[derive(Copy)]
struct S<const N: [f32; N]>();
//~^ ERROR the type of const parameters must not depend on other generic parameters [E0770]
//~^^ ERROR the type of const parameters must not depend on other generic parameters [E0770]
//~^^^ ERROR `[f32; N]` is forbidden as the type of a const generic parameter
//~^^^^ ERROR `[f32; N]` is forbidden as the type of a const generic parameter

#[derive(Copy)]
//~^ ERROR the trait bound `S<N>: Clone` is not satisfied [E0277]
//~^^ the constant `N` is not of type `[f32; {const error}]`
struct S<const N: usize>([f32; N]);
//~^ ERROR the name `S` is defined multiple times [E0428]

fn main() {}
79 changes: 79 additions & 0 deletions tests/ui/coherence/with-negative-coherence-issue-119381.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
error[E0428]: the name `S` is defined multiple times
--> $DIR/with-negative-coherence-issue-119381.rs:13:1
|
LL | struct S<const N: [f32; N]>();
| ------------------------------ previous definition of the type `S` here
...
LL | struct S<const N: usize>([f32; N]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `S` redefined here
|
= note: `S` must be defined only once in the type namespace of this module

error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/with-negative-coherence-issue-119381.rs:4:25
|
LL | struct S<const N: [f32; N]>();
| ^ the type must not depend on the parameter `N`
|
= note: const parameters may not be used in the type of const parameters

error[E0770]: the type of const parameters must not depend on other generic parameters
--> $DIR/with-negative-coherence-issue-119381.rs:4:25
|
LL | struct S<const N: [f32; N]>();
| ^ the type must not depend on the parameter `N`
|
= note: const parameters may not be used in the type of const parameters
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `[f32; N]` is forbidden as the type of a const generic parameter
--> $DIR/with-negative-coherence-issue-119381.rs:4:19
|
LL | struct S<const N: [f32; N]>();
| ^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types

error: `[f32; N]` is forbidden as the type of a const generic parameter
--> $DIR/with-negative-coherence-issue-119381.rs:4:19
|
LL | struct S<const N: [f32; N]>();
| ^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0277]: the trait bound `S<N>: Clone` is not satisfied
--> $DIR/with-negative-coherence-issue-119381.rs:10:10
|
LL | #[derive(Copy)]
| ^^^^ the trait `Clone` is not implemented for `S<N>`
|
note: required by a bound in `Copy`
--> $SRC_DIR/core/src/marker.rs:LL:COL
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `S<N>` with `#[derive(Clone)]`
|
LL + #[derive(Clone)]
LL | struct S<const N: [f32; N]>();
|

error: the constant `N` is not of type `[f32; {const error}]`
--> $DIR/with-negative-coherence-issue-119381.rs:10:10
|
LL | #[derive(Copy)]
| ^^^^ expected `[f32; {const error}]`, found `usize`
|
note: required by a bound in `S`
--> $DIR/with-negative-coherence-issue-119381.rs:4:10
|
LL | struct S<const N: [f32; N]>();
| ^^^^^^^^^^^^^^^^^ required by this bound in `S`
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 7 previous errors

Some errors have detailed explanations: E0277, E0428, E0770.
For more information about an error, try `rustc --explain E0277`.