Skip to content
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

handle ConstKind::Unresolved after monomorphizing #70249

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 19 additions & 24 deletions src/librustc_codegen_ssa/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,26 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
&mut self,
constant: &mir::Constant<'tcx>,
) -> Result<ConstValue<'tcx>, ErrorHandled> {
match constant.literal.val {
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
let substs = self.monomorphize(&substs);
self.cx
.tcx()
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
.map_err(|err| {
if promoted.is_none() {
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
}
err
})
}
match self.monomorphize(&constant.literal).val {
ty::ConstKind::Unevaluated(def_id, substs, promoted) => self
.cx
.tcx()
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
.map_err(|err| {
if promoted.is_none() {
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
}
err
}),
Comment on lines +43 to +56
Copy link
Member

Choose a reason for hiding this comment

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

@oli-obk @wesleywiser @varkor @yodaldevoid NB: this Unevaluated handling should, after #70125 (comment) is fixed, only handle the error case, whereas success should result in monomorphize itself doing the evaluation.

What do you think about merging this PR before that root cause is fixed?

Copy link
Member

@varkor varkor Mar 22, 2020

Choose a reason for hiding this comment

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

I'm happy to merge this interim fix if it addresses one of the existing ICEs 👍

ty::ConstKind::Value(value) => Ok(value),
_ => {
let const_ = self.monomorphize(&constant.literal);
Copy link
Member

Choose a reason for hiding this comment

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

What you could try is first always monomorphize-ing, and then checking for Unevaluated (you'd need to remove let substs = self.monomorphize(&substs);).

It's not the right fix here (see #70125 (comment)), but it could be useful for handling the erroring constant case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, this already seems to fix all tests 🤔 Will still look into your comment on #70125

if let ty::ConstKind::Value(value) = const_.val {
Ok(value)
} else {
span_bug!(constant.span, "encountered bad ConstKind in codegen: {:?}", const_);
}
}
err => span_bug!(
constant.span,
"encountered bad ConstKind after monomorphizing: {:?}",
err
),
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/const-generics/issues/issue-70125.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-pass
#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

const L: usize = 4;

pub trait Print<const N: usize> {
fn print(&self) -> usize {
N
}
}

pub struct Printer;
impl Print<L> for Printer {}

fn main() {
let p = Printer;
assert_eq!(p.print(), 4);
}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/issues/issue-70125.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/issue-70125.rs:2:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

16 changes: 16 additions & 0 deletions src/test/ui/const-generics/normalization_failure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// run-pass
lcnr marked this conversation as resolved.
Show resolved Hide resolved

#![feature(const_generics)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

fn main() {
<()>::foo();
}

trait Foo<const X: usize> {
fn foo() -> usize {
X
}
}

impl Foo<{3}> for () {}
8 changes: 8 additions & 0 deletions src/test/ui/const-generics/normalization_failure.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/normalization_failure.rs:3:12
|
LL | #![feature(const_generics)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default