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

Fix ICE in const check #88592

Merged
merged 2 commits into from
Sep 2, 2021
Merged
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
36 changes: 19 additions & 17 deletions compiler/rustc_mir/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::cast::CastTy;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef};
use rustc_span::{sym, Span, Symbol};
Expand Down Expand Up @@ -793,7 +793,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {

let fn_ty = func.ty(body, tcx);

let (mut callee, substs) = match *fn_ty.kind() {
let (mut callee, mut substs) = match *fn_ty.kind() {
ty::FnDef(def_id, substs) => (def_id, substs),

ty::FnPtr(_) => {
Expand Down Expand Up @@ -846,29 +846,31 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
.iter()
.find(|did| tcx.item_name(**did) == callee_name)
{
// using internal substs is ok here, since this is only
// used for the `resolve` call below
substs = InternalSubsts::identity_for_item(tcx, did);
callee = did;
}
}
_ => {
if !tcx.is_const_fn_raw(callee) {
// At this point, it is only legal when the caller is marked with
// #[default_method_body_is_const], and the callee is in the same
// trait.
let callee_trait = tcx.trait_of_item(callee);
if callee_trait.is_some() {
if tcx.has_attr(caller, sym::default_method_body_is_const) {
if tcx.trait_of_item(caller) == callee_trait {
nonconst_call_permission = true;
}
_ if !tcx.is_const_fn_raw(callee) => {
// At this point, it is only legal when the caller is marked with
// #[default_method_body_is_const], and the callee is in the same
// trait.
let callee_trait = tcx.trait_of_item(callee);
if callee_trait.is_some() {
if tcx.has_attr(caller, sym::default_method_body_is_const) {
if tcx.trait_of_item(caller) == callee_trait {
nonconst_call_permission = true;
}
}
}

if !nonconst_call_permission {
self.check_op(ops::FnCallNonConst);
return;
}
if !nonconst_call_permission {
self.check_op(ops::FnCallNonConst);
return;
}
}
_ => {}
}

// Resolve a trait method call to its concrete implementation, which may be in a
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/const-generics/const_trait_fn-issue-88433.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// build-pass

#![feature(const_trait_impl)]

trait Func<T> {
type Output;

fn call_once(self, arg: T) -> Self::Output;
}


struct Closure;

impl const Func<&usize> for Closure {
type Output = usize;

fn call_once(self, arg: &usize) -> Self::Output {
*arg
}
}

enum Bug<T = [(); Closure.call_once(&0) ]> {
V(T),
}

fn main() {}