Skip to content

Commit ce6d3a7

Browse files
committed
Auto merge of #72080 - matthewjasper:uniform-impl-trait, r=nikomatsakis
Clean up type alias impl trait implementation - Removes special case for top-level impl trait - Removes associated opaque types - Forbid lifetime elision in let position impl trait. This is consistent with the behavior for inferred types. - Handle lifetimes in type alias impl trait more uniformly with other parameters cc #69323 cc #63063 Closes #57188 Closes #62988 Closes #69136 Closes #73061
2 parents 4fb54ed + 8b10d42 commit ce6d3a7

File tree

99 files changed

+923
-851
lines changed

Some content is hidden

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

99 files changed

+923
-851
lines changed

src/librustc_ast/ast.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1860,15 +1860,6 @@ impl TyKind {
18601860
pub fn is_unit(&self) -> bool {
18611861
if let TyKind::Tup(ref tys) = *self { tys.is_empty() } else { false }
18621862
}
1863-
1864-
/// HACK(type_alias_impl_trait, Centril): A temporary crutch used
1865-
/// in lowering to avoid making larger changes there and beyond.
1866-
pub fn opaque_top_hack(&self) -> Option<&GenericBounds> {
1867-
match self {
1868-
Self::ImplTrait(_, bounds) => Some(bounds),
1869-
_ => None,
1870-
}
1871-
}
18721863
}
18731864

18741865
/// Syntax used to declare a trait object.

src/librustc_ast_lowering/item.rs

+38-42
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
2-
use super::{ImplTraitContext, ImplTraitPosition, ImplTraitTypeIdVisitor};
2+
use super::{ImplTraitContext, ImplTraitPosition};
33
use crate::Arena;
44

55
use rustc_ast::ast::*;
66
use rustc_ast::attr;
77
use rustc_ast::node_id::NodeMap;
88
use rustc_ast::ptr::P;
99
use rustc_ast::visit::{self, AssocCtxt, Visitor};
10+
use rustc_data_structures::fx::FxHashSet;
1011
use rustc_errors::struct_span_err;
1112
use rustc_hir as hir;
1213
use rustc_hir::def::{DefKind, Res};
@@ -165,13 +166,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
165166
}
166167
ItemKind::MacroDef(..) => SmallVec::new(),
167168
ItemKind::Fn(..) | ItemKind::Impl { of_trait: None, .. } => smallvec![i.id],
168-
ItemKind::Static(ref ty, ..) | ItemKind::Const(_, ref ty, ..) => {
169-
let mut ids = smallvec![i.id];
170-
if self.sess.features_untracked().impl_trait_in_bindings {
171-
ImplTraitTypeIdVisitor { ids: &mut ids }.visit_ty(ty);
172-
}
173-
ids
174-
}
175169
_ => smallvec![i.id],
176170
};
177171

@@ -292,23 +286,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
292286
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
293287
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
294288
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
295-
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => match ty.kind.opaque_top_hack() {
296-
None => {
297-
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
298-
let generics = self.lower_generics(gen, ImplTraitContext::disallowed());
299-
hir::ItemKind::TyAlias(ty, generics)
300-
}
301-
Some(bounds) => {
302-
let ctx = || ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc);
303-
let ty = hir::OpaqueTy {
304-
generics: self.lower_generics(gen, ctx()),
305-
bounds: self.lower_param_bounds(bounds, ctx()),
306-
impl_trait_fn: None,
307-
origin: hir::OpaqueTyOrigin::TypeAlias,
308-
};
309-
hir::ItemKind::OpaqueTy(ty)
310-
}
311-
},
289+
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => {
290+
// We lower
291+
//
292+
// type Foo = impl Trait
293+
//
294+
// to
295+
//
296+
// type Foo = Foo1
297+
// opaque type Foo1: Trait
298+
let ty = self.lower_ty(
299+
ty,
300+
ImplTraitContext::OtherOpaqueTy {
301+
capturable_lifetimes: &mut FxHashSet::default(),
302+
origin: hir::OpaqueTyOrigin::Misc,
303+
},
304+
);
305+
let generics = self.lower_generics(gen, ImplTraitContext::disallowed());
306+
hir::ItemKind::TyAlias(ty, generics)
307+
}
312308
ItemKind::TyAlias(_, ref generics, _, None) => {
313309
let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err));
314310
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
@@ -438,8 +434,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
438434
span: Span,
439435
body: Option<&Expr>,
440436
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
437+
let mut capturable_lifetimes;
441438
let itctx = if self.sess.features_untracked().impl_trait_in_bindings {
442-
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
439+
capturable_lifetimes = FxHashSet::default();
440+
ImplTraitContext::OtherOpaqueTy {
441+
capturable_lifetimes: &mut capturable_lifetimes,
442+
origin: hir::OpaqueTyOrigin::Misc,
443+
}
443444
} else {
444445
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
445446
};
@@ -844,16 +845,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
844845
let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err));
845846
hir::ImplItemKind::TyAlias(ty)
846847
}
847-
Some(ty) => match ty.kind.opaque_top_hack() {
848-
None => {
849-
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
850-
hir::ImplItemKind::TyAlias(ty)
851-
}
852-
Some(bs) => {
853-
let bs = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
854-
hir::ImplItemKind::OpaqueTy(bs)
855-
}
856-
},
848+
Some(ty) => {
849+
let ty = self.lower_ty(
850+
ty,
851+
ImplTraitContext::OtherOpaqueTy {
852+
capturable_lifetimes: &mut FxHashSet::default(),
853+
origin: hir::OpaqueTyOrigin::Misc,
854+
},
855+
);
856+
hir::ImplItemKind::TyAlias(ty)
857+
}
857858
};
858859
(generics, kind)
859860
}
@@ -887,12 +888,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
887888
defaultness,
888889
kind: match &i.kind {
889890
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
890-
AssocItemKind::TyAlias(.., ty) => {
891-
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
892-
None => hir::AssocItemKind::Type,
893-
Some(_) => hir::AssocItemKind::OpaqueTy,
894-
}
895-
}
891+
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
896892
AssocItemKind::Fn(_, sig, ..) => {
897893
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
898894
}

0 commit comments

Comments
 (0)