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 by disallowing impl Trait in unsupported position #49830

Merged
merged 1 commit into from
Apr 13, 2018
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
53 changes: 36 additions & 17 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,9 @@ impl<'a> LoweringContext<'a> {
_ => None,
}),
|this| {
let itctx = ImplTraitContext::Universal(parent_id);
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
(this.lower_generics(generics), f(this))
(this.lower_generics(generics, itctx), f(this))
})
},
);
Expand Down Expand Up @@ -1043,7 +1044,11 @@ impl<'a> LoweringContext<'a> {
}),
|this| {
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),
abi: f.abi,
decl: this.lower_fn_decl(&f.decl, None, false),
Expand Down Expand Up @@ -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);

// Don't expose `Self` (recovered "keyword used as ident" parse error).
Expand All @@ -1794,7 +1804,6 @@ impl<'a> LoweringContext<'a> {
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);
if !add_bounds.is_empty() {
bounds = bounds
Expand Down Expand Up @@ -1879,6 +1888,7 @@ impl<'a> LoweringContext<'a> {
&mut self,
params: &Vec<GenericParam>,
add_bounds: &NodeMap<Vec<TyParamBound>>,
itctx: ImplTraitContext,
) -> hir::HirVec<hir::GenericParam> {
params
.iter()
Expand All @@ -1889,12 +1899,13 @@ impl<'a> LoweringContext<'a> {
GenericParam::Type(ref ty_param) => hir::GenericParam::Type(self.lower_ty_param(
ty_param,
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
itctx,
)),
})
.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.
// 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
Expand Down Expand Up @@ -1947,7 +1958,7 @@ impl<'a> LoweringContext<'a> {
}

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),
span: g.span,
}
Expand Down Expand Up @@ -1981,6 +1992,7 @@ impl<'a> LoweringContext<'a> {
bound_generic_params: this.lower_generic_params(
bound_generic_params,
&NodeMap(),
ImplTraitContext::Disallowed,
),
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::Disallowed),
bounds: bounds
Expand Down Expand Up @@ -2064,7 +2076,8 @@ impl<'a> LoweringContext<'a> {
p: &PolyTraitRef,
itctx: ImplTraitContext,
) -> 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(
&bound_generic_params
.iter()
Expand Down Expand Up @@ -2216,7 +2229,7 @@ impl<'a> LoweringContext<'a> {
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
ItemKind::Ty(ref t, ref generics) => hir::ItemTy(
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(
hir::EnumDef {
Expand All @@ -2226,15 +2239,21 @@ impl<'a> LoweringContext<'a> {
.map(|x| self.lower_variant(x))
.collect(),
},
self.lower_generics(generics),
self.lower_generics(generics, ImplTraitContext::Disallowed),
),
ItemKind::Struct(ref struct_def, ref generics) => {
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) => {
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(
unsafety,
Expand Down Expand Up @@ -2313,13 +2332,13 @@ impl<'a> LoweringContext<'a> {
hir::ItemTrait(
self.lower_is_auto(is_auto),
self.lower_unsafety(unsafety),
self.lower_generics(generics),
self.lower_generics(generics, ImplTraitContext::Disallowed),
bounds,
items,
)
}
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemTraitAlias(
self.lower_generics(generics),
self.lower_generics(generics, ImplTraitContext::Disallowed),
self.lower_bounds(bounds, ImplTraitContext::Disallowed),
),
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
Expand Down Expand Up @@ -2454,7 +2473,7 @@ impl<'a> LoweringContext<'a> {

let (generics, node) = match i.node {
TraitItemKind::Const(ref ty, ref default) => (
this.lower_generics(&i.generics),
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
hir::TraitItemKind::Const(
this.lower_ty(ty, ImplTraitContext::Disallowed),
default
Expand Down Expand Up @@ -2495,7 +2514,7 @@ impl<'a> LoweringContext<'a> {
)
}
TraitItemKind::Type(ref bounds, ref default) => (
this.lower_generics(&i.generics),
this.lower_generics(&i.generics, ImplTraitContext::Disallowed),
hir::TraitItemKind::Type(
this.lower_bounds(bounds, ImplTraitContext::Disallowed),
default
Expand Down Expand Up @@ -2552,7 +2571,7 @@ impl<'a> LoweringContext<'a> {
ImplItemKind::Const(ref ty, ref 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(
this.lower_ty(ty, ImplTraitContext::Disallowed),
body_id,
Expand Down Expand Up @@ -2583,7 +2602,7 @@ impl<'a> LoweringContext<'a> {
)
}
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)),
),
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
Expand Down
38 changes: 38 additions & 0 deletions src/test/compile-fail/issue-47715.rs
Original file line number Diff line number Diff line change
@@ -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() {
}