Auto merge of #49830 - sinkuu:fix_ice_47715, r=cramertj
Fix ICE by disallowing `impl Trait` in unsupported position Fixes #47715.
This commit is contained in:
commit
7291829268
2 changed files with 74 additions and 17 deletions
|
@ -780,8 +780,9 @@ impl<'a> LoweringContext<'a> {
|
||||||
_ => None,
|
_ => None,
|
||||||
}),
|
}),
|
||||||
|this| {
|
|this| {
|
||||||
|
let itctx = ImplTraitContext::Universal(parent_id);
|
||||||
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
|
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
|
||||||
(this.lower_generics(generics), f(this))
|
(this.lower_generics(generics, itctx), f(this))
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -1043,7 +1044,11 @@ impl<'a> LoweringContext<'a> {
|
||||||
}),
|
}),
|
||||||
|this| {
|
|this| {
|
||||||
hir::TyBareFn(P(hir::BareFnTy {
|
hir::TyBareFn(P(hir::BareFnTy {
|
||||||
generic_params: this.lower_generic_params(&f.generic_params, &NodeMap()),
|
generic_params: this.lower_generic_params(
|
||||||
|
&f.generic_params,
|
||||||
|
&NodeMap(),
|
||||||
|
ImplTraitContext::Disallowed,
|
||||||
|
),
|
||||||
unsafety: this.lower_unsafety(f.unsafety),
|
unsafety: this.lower_unsafety(f.unsafety),
|
||||||
abi: f.abi,
|
abi: f.abi,
|
||||||
decl: this.lower_fn_decl(&f.decl, None, false),
|
decl: this.lower_fn_decl(&f.decl, None, false),
|
||||||
|
@ -1784,7 +1789,12 @@ impl<'a> LoweringContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_ty_param(&mut self, tp: &TyParam, add_bounds: &[TyParamBound]) -> hir::TyParam {
|
fn lower_ty_param(
|
||||||
|
&mut self,
|
||||||
|
tp: &TyParam,
|
||||||
|
add_bounds: &[TyParamBound],
|
||||||
|
itctx: ImplTraitContext,
|
||||||
|
) -> hir::TyParam {
|
||||||
let mut name = self.lower_ident(tp.ident);
|
let mut name = self.lower_ident(tp.ident);
|
||||||
|
|
||||||
// Don't expose `Self` (recovered "keyword used as ident" parse error).
|
// Don't expose `Self` (recovered "keyword used as ident" parse error).
|
||||||
|
@ -1794,7 +1804,6 @@ impl<'a> LoweringContext<'a> {
|
||||||
name = Symbol::gensym("Self");
|
name = Symbol::gensym("Self");
|
||||||
}
|
}
|
||||||
|
|
||||||
let itctx = ImplTraitContext::Universal(self.resolver.definitions().local_def_id(tp.id));
|
|
||||||
let mut bounds = self.lower_bounds(&tp.bounds, itctx);
|
let mut bounds = self.lower_bounds(&tp.bounds, itctx);
|
||||||
if !add_bounds.is_empty() {
|
if !add_bounds.is_empty() {
|
||||||
bounds = bounds
|
bounds = bounds
|
||||||
|
@ -1879,6 +1888,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
params: &Vec<GenericParam>,
|
params: &Vec<GenericParam>,
|
||||||
add_bounds: &NodeMap<Vec<TyParamBound>>,
|
add_bounds: &NodeMap<Vec<TyParamBound>>,
|
||||||
|
itctx: ImplTraitContext,
|
||||||
) -> hir::HirVec<hir::GenericParam> {
|
) -> hir::HirVec<hir::GenericParam> {
|
||||||
params
|
params
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -1889,12 +1899,13 @@ impl<'a> LoweringContext<'a> {
|
||||||
GenericParam::Type(ref ty_param) => hir::GenericParam::Type(self.lower_ty_param(
|
GenericParam::Type(ref ty_param) => hir::GenericParam::Type(self.lower_ty_param(
|
||||||
ty_param,
|
ty_param,
|
||||||
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
|
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
|
||||||
|
itctx,
|
||||||
)),
|
)),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_generics(&mut self, g: &Generics) -> hir::Generics {
|
fn lower_generics(&mut self, g: &Generics, itctx: ImplTraitContext) -> hir::Generics {
|
||||||
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
|
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
|
||||||
// FIXME: This could probably be done with less rightward drift. Also looks like two control
|
// FIXME: This could probably be done with less rightward drift. Also looks like two control
|
||||||
// paths where report_error is called are also the only paths that advance to after
|
// paths where report_error is called are also the only paths that advance to after
|
||||||
|
@ -1947,7 +1958,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
hir::Generics {
|
hir::Generics {
|
||||||
params: self.lower_generic_params(&g.params, &add_bounds),
|
params: self.lower_generic_params(&g.params, &add_bounds, itctx),
|
||||||
where_clause: self.lower_where_clause(&g.where_clause),
|
where_clause: self.lower_where_clause(&g.where_clause),
|
||||||
span: g.span,
|
span: g.span,
|
||||||
}
|
}
|
||||||
|
@ -1981,6 +1992,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
bound_generic_params: this.lower_generic_params(
|
bound_generic_params: this.lower_generic_params(
|
||||||
bound_generic_params,
|
bound_generic_params,
|
||||||
&NodeMap(),
|
&NodeMap(),
|
||||||
|
ImplTraitContext::Disallowed,
|
||||||
),
|
),
|
||||||
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::Disallowed),
|
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::Disallowed),
|
||||||
bounds: bounds
|
bounds: bounds
|
||||||
|
@ -2064,7 +2076,8 @@ impl<'a> LoweringContext<'a> {
|
||||||
p: &PolyTraitRef,
|
p: &PolyTraitRef,
|
||||||
itctx: ImplTraitContext,
|
itctx: ImplTraitContext,
|
||||||
) -> hir::PolyTraitRef {
|
) -> hir::PolyTraitRef {
|
||||||
let bound_generic_params = self.lower_generic_params(&p.bound_generic_params, &NodeMap());
|
let bound_generic_params =
|
||||||
|
self.lower_generic_params(&p.bound_generic_params, &NodeMap(), itctx);
|
||||||
let trait_ref = self.with_parent_impl_lifetime_defs(
|
let trait_ref = self.with_parent_impl_lifetime_defs(
|
||||||
&bound_generic_params
|
&bound_generic_params
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -2217,7 +2230,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
|
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
|
||||||
ItemKind::Ty(ref t, ref generics) => hir::ItemTy(
|
ItemKind::Ty(ref t, ref generics) => hir::ItemTy(
|
||||||
self.lower_ty(t, ImplTraitContext::Disallowed),
|
self.lower_ty(t, ImplTraitContext::Disallowed),
|
||||||
self.lower_generics(generics),
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
),
|
),
|
||||||
ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemEnum(
|
ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemEnum(
|
||||||
hir::EnumDef {
|
hir::EnumDef {
|
||||||
|
@ -2227,15 +2240,21 @@ impl<'a> LoweringContext<'a> {
|
||||||
.map(|x| self.lower_variant(x))
|
.map(|x| self.lower_variant(x))
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
self.lower_generics(generics),
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
),
|
),
|
||||||
ItemKind::Struct(ref struct_def, ref generics) => {
|
ItemKind::Struct(ref struct_def, ref generics) => {
|
||||||
let struct_def = self.lower_variant_data(struct_def);
|
let struct_def = self.lower_variant_data(struct_def);
|
||||||
hir::ItemStruct(struct_def, self.lower_generics(generics))
|
hir::ItemStruct(
|
||||||
|
struct_def,
|
||||||
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
ItemKind::Union(ref vdata, ref generics) => {
|
ItemKind::Union(ref vdata, ref generics) => {
|
||||||
let vdata = self.lower_variant_data(vdata);
|
let vdata = self.lower_variant_data(vdata);
|
||||||
hir::ItemUnion(vdata, self.lower_generics(generics))
|
hir::ItemUnion(
|
||||||
|
vdata,
|
||||||
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
ItemKind::Impl(
|
ItemKind::Impl(
|
||||||
unsafety,
|
unsafety,
|
||||||
|
@ -2314,13 +2333,13 @@ impl<'a> LoweringContext<'a> {
|
||||||
hir::ItemTrait(
|
hir::ItemTrait(
|
||||||
self.lower_is_auto(is_auto),
|
self.lower_is_auto(is_auto),
|
||||||
self.lower_unsafety(unsafety),
|
self.lower_unsafety(unsafety),
|
||||||
self.lower_generics(generics),
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
bounds,
|
bounds,
|
||||||
items,
|
items,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemTraitAlias(
|
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemTraitAlias(
|
||||||
self.lower_generics(generics),
|
self.lower_generics(generics, ImplTraitContext::Disallowed),
|
||||||
self.lower_bounds(bounds, ImplTraitContext::Disallowed),
|
self.lower_bounds(bounds, ImplTraitContext::Disallowed),
|
||||||
),
|
),
|
||||||
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
|
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
|
||||||
|
@ -2455,7 +2474,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
|
|
||||||
let (generics, node) = match i.node {
|
let (generics, node) = match i.node {
|
||||||
TraitItemKind::Const(ref ty, ref default) => (
|
TraitItemKind::Const(ref ty, ref default) => (
|
||||||
this.lower_generics(&i.generics),
|
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
|
||||||
hir::TraitItemKind::Const(
|
hir::TraitItemKind::Const(
|
||||||
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
||||||
default
|
default
|
||||||
|
@ -2496,7 +2515,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
TraitItemKind::Type(ref bounds, ref default) => (
|
TraitItemKind::Type(ref bounds, ref default) => (
|
||||||
this.lower_generics(&i.generics),
|
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
|
||||||
hir::TraitItemKind::Type(
|
hir::TraitItemKind::Type(
|
||||||
this.lower_bounds(bounds, ImplTraitContext::Disallowed),
|
this.lower_bounds(bounds, ImplTraitContext::Disallowed),
|
||||||
default
|
default
|
||||||
|
@ -2553,7 +2572,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
ImplItemKind::Const(ref ty, ref expr) => {
|
ImplItemKind::Const(ref ty, ref expr) => {
|
||||||
let body_id = this.lower_body(None, |this| this.lower_expr(expr));
|
let body_id = this.lower_body(None, |this| this.lower_expr(expr));
|
||||||
(
|
(
|
||||||
this.lower_generics(&i.generics),
|
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
|
||||||
hir::ImplItemKind::Const(
|
hir::ImplItemKind::Const(
|
||||||
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
||||||
body_id,
|
body_id,
|
||||||
|
@ -2584,7 +2603,7 @@ impl<'a> LoweringContext<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ImplItemKind::Type(ref ty) => (
|
ImplItemKind::Type(ref ty) => (
|
||||||
this.lower_generics(&i.generics),
|
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
|
||||||
hir::ImplItemKind::Type(this.lower_ty(ty, ImplTraitContext::Disallowed)),
|
hir::ImplItemKind::Type(this.lower_ty(ty, ImplTraitContext::Disallowed)),
|
||||||
),
|
),
|
||||||
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
||||||
|
|
38
src/test/compile-fail/issue-47715.rs
Normal file
38
src/test/compile-fail/issue-47715.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
trait Foo {}
|
||||||
|
|
||||||
|
trait Bar<T> {}
|
||||||
|
|
||||||
|
trait Iterable {
|
||||||
|
type Item;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Container<T: Iterable<Item = impl Foo>> {
|
||||||
|
//~^ ERROR `impl Trait` not allowed
|
||||||
|
field: T
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Enum<T: Iterable<Item = impl Foo>> {
|
||||||
|
//~^ ERROR `impl Trait` not allowed
|
||||||
|
A(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
union Union<T: Iterable<Item = impl Foo> + Copy> {
|
||||||
|
//~^ ERROR `impl Trait` not allowed
|
||||||
|
x: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Type<T: Iterable<Item = impl Foo>> = T;
|
||||||
|
//~^ ERROR `impl Trait` not allowed
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue