Skip to content

Commit ec82174

Browse files
authored
Rollup merge of #67131 - Centril:item-merge, r=petrochenkov
Merge `TraitItem` & `ImplItem into `AssocItem` In this PR we: - Merge `{Trait,Impl}Item{Kind?}` into `AssocItem{Kind?}` as discussed in #65041 (comment). - This is done by using the cover grammar of both forms. - In particular, it requires that we syntactically allow (under `#[cfg(FALSE)]`): - `default`ness on `trait` items, - `impl` items without a body / definition (`const`, `type`, and `fn`), - and associated `type`s in `impl`s with bounds, e.g., `type Foo: Ord;`. - The syntactic restrictions are replaced by semantic ones in `ast_validation`. - Move syntactic restrictions around C-variadic parameters from the parser into `ast_validation`: - `fn`s in all contexts now syntactically allow `...`, - `...` can occur anywhere in the list syntactically (`fn foo(..., x: usize) {}`), - and `...` can be the sole parameter (`fn foo(...) {}`. r? @petrochenkov
2 parents ba1a488 + 054458b commit ec82174

File tree

65 files changed

+1279
-849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1279
-849
lines changed

src/librustc/hir/lowering.rs

+36-47
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,11 @@ impl<'a> LoweringContext<'a> {
477477
});
478478
}
479479

480-
fn visit_trait_item(&mut self, item: &'tcx TraitItem) {
480+
fn visit_trait_item(&mut self, item: &'tcx AssocItem) {
481481
self.lctx.allocate_hir_id_counter(item.id);
482482

483483
match item.kind {
484-
TraitItemKind::Method(_, None) => {
484+
AssocItemKind::Fn(_, None) => {
485485
// Ignore patterns in trait methods without bodies
486486
self.with_hir_id_owner(None, |this| {
487487
visit::walk_trait_item(this, item)
@@ -493,7 +493,7 @@ impl<'a> LoweringContext<'a> {
493493
}
494494
}
495495

496-
fn visit_impl_item(&mut self, item: &'tcx ImplItem) {
496+
fn visit_impl_item(&mut self, item: &'tcx AssocItem) {
497497
self.lctx.allocate_hir_id_counter(item.id);
498498
self.with_hir_id_owner(Some(item.id), |this| {
499499
visit::walk_impl_item(this, item);
@@ -1211,7 +1211,7 @@ impl<'a> LoweringContext<'a> {
12111211
let ct = self.with_new_scopes(|this| {
12121212
hir::AnonConst {
12131213
hir_id: this.lower_node_id(node_id),
1214-
body: this.lower_const_body(&path_expr),
1214+
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
12151215
}
12161216
});
12171217
return GenericArg::Const(ConstArg {
@@ -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,
@@ -1418,7 +1426,13 @@ impl<'a> LoweringContext<'a> {
14181426
}
14191427
}
14201428
TyKind::Mac(_) => bug!("`TyKind::Mac` should have been expanded by now"),
1421-
TyKind::CVarArgs => bug!("`TyKind::CVarArgs` should have been handled elsewhere"),
1429+
TyKind::CVarArgs => {
1430+
self.sess.delay_span_bug(
1431+
t.span,
1432+
"`TyKind::CVarArgs` should have been handled elsewhere",
1433+
);
1434+
hir::TyKind::Err
1435+
}
14221436
};
14231437

14241438
hir::Ty {
@@ -2084,32 +2098,19 @@ impl<'a> LoweringContext<'a> {
20842098
.iter()
20852099
.map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed()))
20862100
.collect();
2087-
let mk_tup = |this: &mut Self, tys, span| {
2088-
hir::Ty { kind: hir::TyKind::Tup(tys), hir_id: this.next_id(), span }
2101+
let output_ty = match output {
2102+
FunctionRetTy::Ty(ty) => this.lower_ty(&ty, ImplTraitContext::disallowed()),
2103+
FunctionRetTy::Default(_) => P(this.ty_tup(span, hir::HirVec::new())),
2104+
};
2105+
let args = hir_vec![GenericArg::Type(this.ty_tup(span, inputs))];
2106+
let binding = hir::TypeBinding {
2107+
hir_id: this.next_id(),
2108+
ident: Ident::with_dummy_span(FN_OUTPUT_NAME),
2109+
span: output_ty.span,
2110+
kind: hir::TypeBindingKind::Equality { ty: output_ty },
20892111
};
20902112
(
2091-
hir::GenericArgs {
2092-
args: hir_vec![GenericArg::Type(mk_tup(this, inputs, span))],
2093-
bindings: hir_vec![
2094-
hir::TypeBinding {
2095-
hir_id: this.next_id(),
2096-
ident: Ident::with_dummy_span(FN_OUTPUT_NAME),
2097-
kind: hir::TypeBindingKind::Equality {
2098-
ty: output
2099-
.as_ref()
2100-
.map(|ty| this.lower_ty(
2101-
&ty,
2102-
ImplTraitContext::disallowed()
2103-
))
2104-
.unwrap_or_else(||
2105-
P(mk_tup(this, hir::HirVec::new(), span))
2106-
),
2107-
},
2108-
span: output.as_ref().map_or(span, |ty| ty.span),
2109-
}
2110-
],
2111-
parenthesized: true,
2112-
},
2113+
hir::GenericArgs { args, bindings: hir_vec![binding], parenthesized: true },
21132114
false,
21142115
)
21152116
}
@@ -2474,17 +2475,13 @@ impl<'a> LoweringContext<'a> {
24742475
})
24752476
);
24762477

2477-
// Create the `Foo<...>` refernece itself. Note that the `type
2478+
// Create the `Foo<...>` reference itself. Note that the `type
24782479
// Foo = impl Trait` is, internally, created as a child of the
24792480
// async fn, so the *type parameters* are inherited. It's
24802481
// only the lifetime parameters that we must supply.
24812482
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-
}))
2483+
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
2484+
hir::FunctionRetTy::Return(P(opaque_ty))
24882485
}
24892486

24902487
/// Transforms `-> T` into `Future<Output = T>`
@@ -2496,16 +2493,8 @@ impl<'a> LoweringContext<'a> {
24962493
) -> hir::GenericBound {
24972494
// Compute the `T` in `Future<Output = T>` from the return type.
24982495
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-
}
2496+
FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))),
2497+
FunctionRetTy::Default(ret_ty_span) => P(self.ty_tup(*ret_ty_span, hir_vec![])),
25092498
};
25102499

25112500
// "<Output = T>"
@@ -3017,7 +3006,7 @@ impl<'a> LoweringContext<'a> {
30173006
self.with_new_scopes(|this| {
30183007
hir::AnonConst {
30193008
hir_id: this.lower_node_id(c.id),
3020-
body: this.lower_const_body(&c.value),
3009+
body: this.lower_const_body(c.value.span, Some(&c.value)),
30213010
}
30223011
})
30233012
}

0 commit comments

Comments
 (0)