Skip to content

Commit 3b82e4c

Browse files
committed
Auto merge of #45723 - sinkuu:ice_45493, r=arielb1
Fix MIR inlining panic in generic function MIR inlining calls `Instance::resolve` with a substs containing param, and `trans_apply_param_substs` panics. ~~This PR fixes it by making `Instance::resolve` return `None` if `substs.has_param_types()`, though I'm not sure if this is a right fix.~~ Fixes #45493.
2 parents 666687a + afb52e1 commit 3b82e4c

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/librustc/traits/trans/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
9999
let substituted = self.erase_regions(&substituted);
100100
AssociatedTypeNormalizer::new(self).fold(&substituted)
101101
}
102+
103+
pub fn trans_apply_param_substs_env<T>(
104+
self,
105+
param_substs: &Substs<'tcx>,
106+
param_env: ty::ParamEnv<'tcx>,
107+
value: &T,
108+
) -> T
109+
where
110+
T: TransNormalize<'tcx>,
111+
{
112+
debug!(
113+
"apply_param_substs_env(param_substs={:?}, value={:?}, param_env={:?})",
114+
param_substs,
115+
value,
116+
param_env,
117+
);
118+
let substituted = value.subst(self, param_substs);
119+
let substituted = self.erase_regions(&substituted);
120+
AssociatedTypeNormalizerEnv::new(self, param_env).fold(&substituted)
121+
}
102122
}
103123

104124
struct AssociatedTypeNormalizer<'a, 'gcx: 'a> {
@@ -134,6 +154,40 @@ impl<'a, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizer<'a, 'gcx> {
134154
}
135155
}
136156

157+
struct AssociatedTypeNormalizerEnv<'a, 'gcx: 'a> {
158+
tcx: TyCtxt<'a, 'gcx, 'gcx>,
159+
param_env: ty::ParamEnv<'gcx>,
160+
}
161+
162+
impl<'a, 'gcx> AssociatedTypeNormalizerEnv<'a, 'gcx> {
163+
fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>, param_env: ty::ParamEnv<'gcx>) -> Self {
164+
Self { tcx, param_env }
165+
}
166+
167+
fn fold<T: TypeFoldable<'gcx>>(&mut self, value: &T) -> T {
168+
if !value.has_projections() {
169+
value.clone()
170+
} else {
171+
value.fold_with(self)
172+
}
173+
}
174+
}
175+
176+
impl<'a, 'gcx> TypeFolder<'gcx, 'gcx> for AssociatedTypeNormalizerEnv<'a, 'gcx> {
177+
fn tcx<'c>(&'c self) -> TyCtxt<'c, 'gcx, 'gcx> {
178+
self.tcx
179+
}
180+
181+
fn fold_ty(&mut self, ty: Ty<'gcx>) -> Ty<'gcx> {
182+
if !ty.has_projections() {
183+
ty
184+
} else {
185+
debug!("AssociatedTypeNormalizerEnv: ty={:?}", ty);
186+
self.tcx.normalize_associated_type_in_env(&ty, self.param_env)
187+
}
188+
}
189+
}
190+
137191
// Implement DepTrackingMapConfig for `trait_cache`
138192
pub struct TraitSelectionCache<'tcx> {
139193
data: PhantomData<&'tcx ()>

src/librustc/ty/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
144144
resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
145145
} else {
146146
let ty = tcx.type_of(def_id);
147-
let item_type = tcx.trans_apply_param_substs(substs, &ty);
147+
let item_type = tcx.trans_apply_param_substs_env(substs, param_env, &ty);
148148

149149
let def = match item_type.sty {
150150
ty::TyFnDef(..) if {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:-Zmir-opt-level=2
12+
13+
trait Array {
14+
type Item;
15+
}
16+
17+
fn foo<A: Array>() {
18+
let _: *mut A::Item = std::ptr::null_mut();
19+
}
20+
21+
struct Foo;
22+
impl Array for Foo { type Item = i32; }
23+
24+
fn main() {
25+
foo::<Foo>();
26+
}

0 commit comments

Comments
 (0)