Skip to content

Commit 3907376

Browse files
committed
Unify {Trait,Impl}ItemKind::TyAlias structures.
1 parent c02fd31 commit 3907376

File tree

16 files changed

+209
-54
lines changed

16 files changed

+209
-54
lines changed

src/librustc/hir/lowering.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,14 @@ impl<'a> LoweringContext<'a> {
12531253
ty
12541254
}
12551255

1256+
fn ty(&mut self, span: Span, kind: hir::TyKind) -> hir::Ty {
1257+
hir::Ty { hir_id: self.next_id(), kind, span }
1258+
}
1259+
1260+
fn ty_tup(&mut self, span: Span, tys: HirVec<hir::Ty>) -> hir::Ty {
1261+
self.ty(span, hir::TyKind::Tup(tys))
1262+
}
1263+
12561264
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_>) -> hir::Ty {
12571265
let kind = match t.kind {
12581266
TyKind::Infer => hir::TyKind::Infer,
@@ -2084,12 +2092,9 @@ impl<'a> LoweringContext<'a> {
20842092
.iter()
20852093
.map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed()))
20862094
.collect();
2087-
let mk_tup = |this: &mut Self, tys, span| {
2088-
hir::Ty { kind: hir::TyKind::Tup(tys), hir_id: this.next_id(), span }
2089-
};
20902095
(
20912096
hir::GenericArgs {
2092-
args: hir_vec![GenericArg::Type(mk_tup(this, inputs, span))],
2097+
args: hir_vec![GenericArg::Type(this.ty_tup(span, inputs))],
20932098
bindings: hir_vec![
20942099
hir::TypeBinding {
20952100
hir_id: this.next_id(),
@@ -2102,7 +2107,7 @@ impl<'a> LoweringContext<'a> {
21022107
ImplTraitContext::disallowed()
21032108
))
21042109
.unwrap_or_else(||
2105-
P(mk_tup(this, hir::HirVec::new(), span))
2110+
P(this.ty_tup(span, hir::HirVec::new()))
21062111
),
21072112
},
21082113
span: output.as_ref().map_or(span, |ty| ty.span),
@@ -2474,17 +2479,13 @@ impl<'a> LoweringContext<'a> {
24742479
})
24752480
);
24762481

2477-
// Create the `Foo<...>` refernece itself. Note that the `type
2482+
// Create the `Foo<...>` reference itself. Note that the `type
24782483
// Foo = impl Trait` is, internally, created as a child of the
24792484
// async fn, so the *type parameters* are inherited. It's
24802485
// only the lifetime parameters that we must supply.
24812486
let opaque_ty_ref = hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, generic_args.into());
2482-
2483-
hir::FunctionRetTy::Return(P(hir::Ty {
2484-
kind: opaque_ty_ref,
2485-
span: opaque_ty_span,
2486-
hir_id: self.next_id(),
2487-
}))
2487+
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
2488+
hir::FunctionRetTy::Return(P(opaque_ty))
24882489
}
24892490

24902491
/// Transforms `-> T` into `Future<Output = T>`
@@ -2496,16 +2497,8 @@ impl<'a> LoweringContext<'a> {
24962497
) -> hir::GenericBound {
24972498
// Compute the `T` in `Future<Output = T>` from the return type.
24982499
let output_ty = match output {
2499-
FunctionRetTy::Ty(ty) => {
2500-
self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id)))
2501-
}
2502-
FunctionRetTy::Default(ret_ty_span) => {
2503-
P(hir::Ty {
2504-
hir_id: self.next_id(),
2505-
kind: hir::TyKind::Tup(hir_vec![]),
2506-
span: *ret_ty_span,
2507-
})
2508-
}
2500+
FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))),
2501+
FunctionRetTy::Default(ret_ty_span) => P(self.ty_tup(*ret_ty_span, hir_vec![])),
25092502
};
25102503

25112504
// "<Output = T>"

src/librustc/hir/lowering/item.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -932,16 +932,21 @@ impl LoweringContext<'_> {
932932

933933
(generics, hir::ImplItemKind::Method(sig, body_id))
934934
}
935-
ImplItemKind::TyAlias(ref ty) => {
935+
ImplItemKind::TyAlias(_, ref ty) => {
936936
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
937-
let kind = match ty.kind.opaque_top_hack() {
937+
let kind = match ty {
938938
None => {
939-
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
940-
hir::ImplItemKind::TyAlias(ty)
939+
hir::ImplItemKind::TyAlias(P(self.ty(i.span, hir::TyKind::Err)))
941940
}
942-
Some(bs) => {
943-
let bounds = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
944-
hir::ImplItemKind::OpaqueTy(bounds)
941+
Some(ty) => match ty.kind.opaque_top_hack() {
942+
None => {
943+
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
944+
hir::ImplItemKind::TyAlias(ty)
945+
}
946+
Some(bs) => {
947+
let bs = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
948+
hir::ImplItemKind::OpaqueTy(bs)
949+
}
945950
}
946951
};
947952
(generics, kind)
@@ -972,7 +977,10 @@ impl LoweringContext<'_> {
972977
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
973978
kind: match &i.kind {
974979
ImplItemKind::Const(..) => hir::AssocItemKind::Const,
975-
ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() {
980+
ImplItemKind::TyAlias(_, ty) => match ty
981+
.as_deref()
982+
.and_then(|ty| ty.kind.opaque_top_hack())
983+
{
976984
None => hir::AssocItemKind::Type,
977985
Some(_) => hir::AssocItemKind::OpaqueTy,
978986
},

src/librustc_parse/parser/item.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,7 @@ impl<'a> Parser<'a> {
697697
let vis = self.parse_visibility(FollowedByType::No)?;
698698
let defaultness = self.parse_defaultness();
699699
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
700-
let (name, ty, generics) = self.parse_type_alias()?;
701-
(name, ast::ImplItemKind::TyAlias(ty), generics)
700+
self.parse_impl_assoc_ty()?
702701
} else if self.is_const_item() {
703702
self.parse_impl_const()?
704703
} else if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(&vis), at_end)? {
@@ -766,6 +765,31 @@ impl<'a> Parser<'a> {
766765
Ok((ident, ImplItemKind::Const(ty, expr), Generics::default()))
767766
}
768767

768+
/// Parses the following grammar:
769+
///
770+
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
771+
fn parse_impl_assoc_ty(&mut self) -> PResult<'a, (Ident, ImplItemKind, Generics)> {
772+
let ident = self.parse_ident()?;
773+
let mut generics = self.parse_generics()?;
774+
775+
// Parse optional colon and param bounds.
776+
let bounds = if self.eat(&token::Colon) {
777+
self.parse_generic_bounds(None)?
778+
} else {
779+
Vec::new()
780+
};
781+
generics.where_clause = self.parse_where_clause()?;
782+
783+
let default = if self.eat(&token::Eq) {
784+
Some(self.parse_ty()?)
785+
} else {
786+
None
787+
};
788+
self.expect_semi()?;
789+
790+
Ok((ident, ImplItemKind::TyAlias(bounds, default), generics))
791+
}
792+
769793
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
770794
fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
771795
// Parse optional `auto` prefix.
@@ -924,7 +948,7 @@ impl<'a> Parser<'a> {
924948

925949
/// Parses the following grammar:
926950
///
927-
/// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
951+
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
928952
fn parse_trait_item_assoc_ty(&mut self) -> PResult<'a, (Ident, TraitItemKind, Generics)> {
929953
let ident = self.parse_ident()?;
930954
let mut generics = self.parse_generics()?;

src/librustc_passes/ast_validation.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,17 @@ impl<'a> AstValidator<'a> {
295295
)
296296
.emit();
297297
}
298+
299+
fn check_impl_assoc_type_no_bounds(&self, bounds: &[GenericBound]) {
300+
let span = match bounds {
301+
[] => return,
302+
[b0] => b0.span(),
303+
[b0, .., bl] => b0.span().to(bl.span()),
304+
};
305+
self.err_handler()
306+
.struct_span_err(span, "bounds on associated `type`s in `impl`s have no effect")
307+
.emit();
308+
}
298309
}
299310

300311
enum GenericPosition {
@@ -770,6 +781,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
770781
self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
771782
self.check_fn_decl(&sig.decl);
772783
}
784+
ImplItemKind::TyAlias(bounds, body) => {
785+
self.check_impl_item_provided(ii.span, body, "type", " = <type>;");
786+
self.check_impl_assoc_type_no_bounds(bounds);
787+
}
773788
_ => {}
774789
}
775790
visit::walk_impl_item(self, ii);

src/librustc_passes/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#![feature(in_band_lifetimes)]
1010
#![feature(nll)]
11+
#![feature(slice_patterns)]
1112

1213
#![recursion_limit="256"]
1314

src/librustc_resolve/late.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
11191119

11201120
visit::walk_impl_item(this, impl_item);
11211121
}
1122-
ImplItemKind::TyAlias(ref ty) => {
1122+
ImplItemKind::TyAlias(_, Some(ref ty)) => {
11231123
// If this is a trait impl, ensure the type
11241124
// exists in trait
11251125
this.check_trait_item(impl_item.ident,
@@ -1129,6 +1129,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
11291129

11301130
this.visit_ty(ty);
11311131
}
1132+
ImplItemKind::TyAlias(_, None) => {}
11321133
ImplItemKind::Macro(_) =>
11331134
panic!("unexpanded macro in resolve!"),
11341135
}

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
11271127
impl_item.span,
11281128
);
11291129
}
1130-
ast::ImplItemKind::TyAlias(ref ty) => {
1130+
ast::ImplItemKind::TyAlias(_, None) => {}
1131+
ast::ImplItemKind::TyAlias(_, Some(ref ty)) => {
11311132
// FIXME: uses of the assoc type should ideally point to this
11321133
// 'def' and the name here should be a ref to the def in the
11331134
// trait.

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ pub struct ImplItem<K = ImplItemKind> {
16381638
pub enum ImplItemKind {
16391639
Const(P<Ty>, Option<P<Expr>>),
16401640
Method(FnSig, Option<P<Block>>),
1641-
TyAlias(P<Ty>),
1641+
TyAlias(GenericBounds, Option<P<Ty>>),
16421642
Macro(Mac),
16431643
}
16441644

src/libsyntax/feature_gate/check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
612612
"C-variadic functions are unstable");
613613
}
614614
}
615-
ast::ImplItemKind::TyAlias(ref ty) => {
616-
self.check_impl_trait(ty);
615+
ast::ImplItemKind::TyAlias(_, ref ty) => {
616+
if let Some(ty) = ty {
617+
self.check_impl_trait(ty);
618+
}
617619
self.check_gat(&ii.generics, ii.span);
618620
}
619621
_ => {}

src/libsyntax/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,10 @@ pub fn noop_flat_map_impl_item<T: MutVisitor>(mut item: ImplItem, visitor: &mut
987987
visit_fn_sig(sig, visitor);
988988
visit_opt(body, |body| visitor.visit_block(body));
989989
}
990-
ImplItemKind::TyAlias(ty) => visitor.visit_ty(ty),
990+
ImplItemKind::TyAlias(bounds, ty) => {
991+
visit_bounds(bounds, visitor);
992+
visit_opt(ty, |ty| visitor.visit_ty(ty));
993+
}
991994
ImplItemKind::Macro(mac) => visitor.visit_mac(mac),
992995
}
993996
visitor.visit_span(span);

0 commit comments

Comments
 (0)