]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_ast_lowering/src/lib.rs
New upstream version 1.48.0+dfsg1
[rustc.git] / compiler / rustc_ast_lowering / src / lib.rs
CommitLineData
041b39d2
XL
1//! Lowers the AST to the HIR.
2//!
3//! Since the AST and HIR are fairly similar, this is mostly a simple procedure,
4//! much like a fold. Where lowering involves a bit more work things get more
5//! interesting and there are some invariants you should know about. These mostly
9fa01778 6//! concern spans and IDs.
041b39d2
XL
7//!
8//! Spans are assigned to AST nodes during parsing and then are modified during
9//! expansion to indicate the origin of a node and the process it went through
9fa01778 10//! being expanded. IDs are assigned to AST nodes just before lowering.
041b39d2 11//!
9fa01778 12//! For the simpler lowering steps, IDs and spans should be preserved. Unlike
041b39d2
XL
13//! expansion we do not preserve the process of lowering in the spans, so spans
14//! should not be modified here. When creating a new node (as opposed to
9fa01778 15//! 'folding' an existing one), then you create a new ID using `next_id()`.
041b39d2 16//!
9fa01778 17//! You must ensure that IDs are unique. That means that you should only use the
dc9dc135 18//! ID from an AST node in a single HIR node (you can assume that AST node-IDs
9fa01778
XL
19//! are unique). Every new node must have a unique ID. Avoid cloning HIR nodes.
20//! If you do, you must then set the new node's ID to a fresh one.
041b39d2
XL
21//!
22//! Spans are used for error messages and for tools to map semantics back to
9fa01778 23//! source code. It is therefore not as important with spans as IDs to be strict
041b39d2
XL
24//! about use (you can't break the compiler by screwing up a span). Obviously, a
25//! HIR node can only have a single span. But multiple nodes can have the same
26//! span and spans don't need to be kept in order, etc. Where code is preserved
27//! by lowering, it should have the same span as in the AST. Where HIR nodes are
28//! new it is probably best to give a span for the whole AST node being lowered.
29//! All nodes should have real spans, don't use dummy spans. Tools are likely to
30//! get confused if the spans from leaf AST nodes occur in multiple places
31//! in the HIR, especially for multiple identifiers.
e9174d1e 32
dfeec247
XL
33#![feature(array_value_iter)]
34#![feature(crate_visibility_modifier)]
ba9703b0 35#![feature(or_patterns)]
74b04a01 36#![recursion_limit = "256"]
416331ca 37
74b04a01 38use rustc_ast::node_id::NodeMap;
f035d41b
XL
39use rustc_ast::token::{self, DelimToken, Nonterminal, Token};
40use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
74b04a01
XL
41use rustc_ast::visit::{self, AssocCtxt, Visitor};
42use rustc_ast::walk_list;
3dfed10e 43use rustc_ast::{self as ast, *};
74b04a01 44use rustc_ast_pretty::pprust;
dfeec247 45use rustc_data_structures::captures::Captures;
b7449926 46use rustc_data_structures::fx::FxHashSet;
dc9dc135 47use rustc_data_structures::sync::Lrc;
dfeec247
XL
48use rustc_errors::struct_span_err;
49use rustc_hir as hir;
50use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
ba9703b0
XL
51use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
52use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
dfeec247
XL
53use rustc_hir::intravisit;
54use rustc_hir::{ConstArg, GenericArg, ParamName};
f035d41b 55use rustc_index::vec::{Idx, IndexVec};
dfeec247 56use rustc_session::config::nightly_options;
74b04a01
XL
57use rustc_session::lint::{builtin::BARE_TRAIT_OBJECTS, BuiltinLintDiagnostics, LintBuffer};
58use rustc_session::parse::ParseSess;
dfeec247
XL
59use rustc_session::Session;
60use rustc_span::hygiene::ExpnId;
61use rustc_span::source_map::{respan, DesugaringKind, ExpnData, ExpnKind};
f9f354fc 62use rustc_span::symbol::{kw, sym, Ident, Symbol};
dfeec247 63use rustc_span::Span;
b039eaaf 64
dfeec247
XL
65use smallvec::{smallvec, SmallVec};
66use std::collections::BTreeMap;
67use std::mem;
3dfed10e 68use tracing::{debug, trace};
dfeec247
XL
69
70macro_rules! arena_vec {
71 ($this:expr; $($x:expr),*) => ({
72 let a = [$($x),*];
73 $this.arena.alloc_from_iter(std::array::IntoIter::new(a))
74 });
75}
76
77mod expr;
78mod item;
79mod pat;
80mod path;
60c5eb7d 81
cc61c64b
XL
82const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
83
f035d41b 84rustc_hir::arena_types!(rustc_arena::declare_arena, [], 'tcx);
ba9703b0 85
dfeec247 86struct LoweringContext<'a, 'hir: 'a> {
e1599b0c 87 /// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
9e0c209e 88 sess: &'a Session,
041b39d2 89
f035d41b 90 resolver: &'a mut dyn ResolverAstLowering,
476ff2be 91
e74abb32
XL
92 /// HACK(Centril): there is a cyclic dependency between the parser and lowering
93 /// if we don't have this function pointer. To avoid that dependency so that
ba9703b0 94 /// librustc_middle is independent of the parser, we use dynamic dispatch here.
e74abb32
XL
95 nt_to_tokenstream: NtToTokenstream,
96
dfeec247
XL
97 /// Used to allocate HIR nodes
98 arena: &'hir Arena<'hir>,
99
476ff2be 100 /// The items being lowered are collected here.
dfeec247 101 items: BTreeMap<hir::HirId, hir::Item<'hir>>,
476ff2be 102
dfeec247
XL
103 trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem<'hir>>,
104 impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
105 bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
106 exported_macros: Vec<hir::MacroDef<'hir>>,
416331ca 107 non_exported_macro_attrs: Vec<ast::Attribute>,
8bb4bdeb 108
532ac7d7 109 trait_impls: BTreeMap<DefId, Vec<hir::HirId>>,
8bb4bdeb 110
e74abb32 111 modules: BTreeMap<hir::HirId, hir::ModuleItems>,
0731742a 112
dc9dc135 113 generator_kind: Option<hir::GeneratorKind>,
48663c56 114
ba9703b0
XL
115 /// When inside an `async` context, this is the `HirId` of the
116 /// `task_context` local bound to the resume argument of the generator.
117 task_context: Option<hir::HirId>,
118
48663c56
XL
119 /// Used to get the current `fn`'s def span to point to when using `await`
120 /// outside of an `async fn`.
121 current_item: Option<Span>,
ea8adc8c 122
cc61c64b 123 catch_scopes: Vec<NodeId>,
8bb4bdeb
XL
124 loop_scopes: Vec<NodeId>,
125 is_in_loop_condition: bool,
abe05a73 126 is_in_trait_impl: bool,
dc9dc135 127 is_in_dyn_type: bool,
32a655c1 128
0531ce1d
XL
129 /// What to do when we encounter either an "anonymous lifetime
130 /// reference". The term "anonymous" is meant to encompass both
131 /// `'_` lifetimes as well as fully elided cases where nothing is
132 /// written at all (e.g., `&T` or `std::cell::Ref<T>`).
133 anonymous_lifetime_mode: AnonymousLifetimeMode,
134
532ac7d7
XL
135 /// Used to create lifetime definitions from in-band lifetime usages.
136 /// e.g., `fn foo(x: &'x u8) -> &'x u8` to `fn foo<'x>(x: &'x u8) -> &'x u8`
137 /// When a named lifetime is encountered in a function or impl header and
138 /// has not been defined
139 /// (i.e., it doesn't appear in the in_scope_lifetimes list), it is added
140 /// to this list. The results of this list are then added to the list of
141 /// lifetime definitions in the corresponding impl or function generics.
8faf50e0 142 lifetimes_to_define: Vec<(Span, ParamName)>,
0531ce1d 143
e1599b0c 144 /// `true` if in-band lifetimes are being collected. This is used to
532ac7d7
XL
145 /// indicate whether or not we're in a place where new lifetimes will result
146 /// in in-band lifetime definitions, such a function or an impl header,
147 /// including implicit lifetimes from `impl_header_lifetime_elision`.
ff7c6d11 148 is_collecting_in_band_lifetimes: bool,
0531ce1d 149
532ac7d7
XL
150 /// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
151 /// When `is_collectin_in_band_lifetimes` is true, each lifetime is checked
152 /// against this list to see if it is already in-scope, or if a definition
153 /// needs to be created for it.
e1599b0c 154 ///
ba9703b0 155 /// We always store a `normalize_to_macros_2_0()` version of the param-name in this
e1599b0c
XL
156 /// vector.
157 in_scope_lifetimes: Vec<ParamName>,
ff7c6d11 158
e74abb32 159 current_module: hir::HirId,
0731742a 160
32a655c1 161 type_def_lifetime_params: DefIdMap<usize>,
cc61c64b 162
ba9703b0 163 current_hir_id_owner: Vec<(LocalDefId, u32)>,
cc61c64b 164 item_local_id_counters: NodeMap<u32>,
ba9703b0 165 node_id_to_hir_id: IndexVec<NodeId, Option<hir::HirId>>,
dc9dc135
XL
166
167 allow_try_trait: Option<Lrc<[Symbol]>>,
168 allow_gen_future: Option<Lrc<[Symbol]>>,
b039eaaf
SL
169}
170
f035d41b 171pub trait ResolverAstLowering {
dfeec247
XL
172 fn def_key(&mut self, id: DefId) -> DefKey;
173
174 fn item_generics_num_lifetimes(&self, def: DefId, sess: &Session) -> usize;
e74abb32 175
e1599b0c 176 /// Obtains resolution for a `NodeId` with a single resolution.
48663c56
XL
177 fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes>;
178
e1599b0c 179 /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
48663c56 180 fn get_import_res(&mut self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;
a7813a04 181
e1599b0c 182 /// Obtains resolution for a label with the given `NodeId`.
48663c56 183 fn get_label_res(&mut self, id: NodeId) -> Option<NodeId>;
94b46f34 184
041b39d2
XL
185 /// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
186 /// This should only return `None` during testing.
9e0c209e 187 fn definitions(&mut self) -> &mut Definitions;
2c00a5a8 188
dfeec247 189 fn lint_buffer(&mut self) -> &mut LintBuffer;
60c5eb7d
XL
190
191 fn next_node_id(&mut self) -> NodeId;
f035d41b
XL
192
193 fn trait_map(&self) -> &NodeMap<Vec<hir::TraitCandidate>>;
194
195 fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId>;
196
197 fn local_def_id(&self, node: NodeId) -> LocalDefId;
198
199 fn create_def(
200 &mut self,
201 parent: LocalDefId,
202 node_id: ast::NodeId,
203 data: DefPathData,
204 expn_id: ExpnId,
205 span: Span,
206 ) -> LocalDefId;
a7813a04 207}
b039eaaf 208
e74abb32
XL
209type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
210
dc9dc135
XL
211/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
212/// and if so, what meaning it has.
8faf50e0 213#[derive(Debug)]
dfeec247 214enum ImplTraitContext<'b, 'a> {
abe05a73
XL
215 /// Treat `impl Trait` as shorthand for a new universal generic parameter.
216 /// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
217 /// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
218 ///
0bf4aa26 219 /// Newly generated parameters should be inserted into the given `Vec`.
dfeec247 220 Universal(&'b mut Vec<hir::GenericParam<'a>>),
abe05a73 221
416331ca 222 /// Treat `impl Trait` as shorthand for a new opaque type.
abe05a73 223 /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
416331ca 224 /// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
94b46f34 225 ///
f035d41b
XL
226 ReturnPositionOpaqueTy {
227 /// `DefId` for the parent function, used to look up necessary
228 /// information later.
229 fn_def_id: DefId,
230 /// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
231 origin: hir::OpaqueTyOrigin,
232 },
233 /// Impl trait in type aliases, consts and statics.
234 OtherOpaqueTy {
235 /// Set of lifetimes that this opaque type can capture, if it uses
236 /// them. This includes lifetimes bound since we entered this context.
237 /// For example, in
238 ///
239 /// type A<'b> = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
240 ///
241 /// the inner opaque type captures `'a` because it uses it. It doesn't
242 /// need to capture `'b` because it already inherits the lifetime
243 /// parameter from `A`.
244 // FIXME(impl_trait): but `required_region_bounds` will ICE later
245 // anyway.
246 capturable_lifetimes: &'b mut FxHashSet<hir::LifetimeName>,
247 /// Origin: Either OpaqueTyOrigin::Misc or OpaqueTyOrigin::Binding,
248 origin: hir::OpaqueTyOrigin,
249 },
abe05a73 250 /// `impl Trait` is not accepted in this position.
0bf4aa26
XL
251 Disallowed(ImplTraitPosition),
252}
253
dc9dc135 254/// Position in which `impl Trait` is disallowed.
0bf4aa26
XL
255#[derive(Debug, Copy, Clone, PartialEq, Eq)]
256enum ImplTraitPosition {
dc9dc135 257 /// Disallowed in `let` / `const` / `static` bindings.
0bf4aa26 258 Binding,
dc9dc135
XL
259
260 /// All other posiitons.
0bf4aa26 261 Other,
abe05a73
XL
262}
263
dfeec247 264impl<'a> ImplTraitContext<'_, 'a> {
0bf4aa26
XL
265 #[inline]
266 fn disallowed() -> Self {
267 ImplTraitContext::Disallowed(ImplTraitPosition::Other)
268 }
269
dfeec247 270 fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
8faf50e0
XL
271 use self::ImplTraitContext::*;
272 match self {
273 Universal(params) => Universal(params),
f035d41b
XL
274 ReturnPositionOpaqueTy { fn_def_id, origin } => {
275 ReturnPositionOpaqueTy { fn_def_id: *fn_def_id, origin: *origin }
276 }
277 OtherOpaqueTy { capturable_lifetimes, origin } => {
278 OtherOpaqueTy { capturable_lifetimes, origin: *origin }
279 }
0bf4aa26 280 Disallowed(pos) => Disallowed(*pos),
8faf50e0
XL
281 }
282 }
283}
284
dfeec247
XL
285pub fn lower_crate<'a, 'hir>(
286 sess: &'a Session,
dfeec247 287 krate: &'a Crate,
f035d41b 288 resolver: &'a mut dyn ResolverAstLowering,
e74abb32 289 nt_to_tokenstream: NtToTokenstream,
dfeec247
XL
290 arena: &'hir Arena<'hir>,
291) -> hir::Crate<'hir> {
dfeec247 292 let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
e74abb32 293
a7813a04 294 LoweringContext {
041b39d2 295 sess,
041b39d2 296 resolver,
e74abb32 297 nt_to_tokenstream,
dfeec247 298 arena,
476ff2be 299 items: BTreeMap::new(),
32a655c1 300 trait_items: BTreeMap::new(),
476ff2be 301 impl_items: BTreeMap::new(),
8bb4bdeb
XL
302 bodies: BTreeMap::new(),
303 trait_impls: BTreeMap::new(),
0731742a 304 modules: BTreeMap::new(),
8bb4bdeb 305 exported_macros: Vec::new(),
416331ca 306 non_exported_macro_attrs: Vec::new(),
cc61c64b 307 catch_scopes: Vec::new(),
8bb4bdeb
XL
308 loop_scopes: Vec::new(),
309 is_in_loop_condition: false,
dc9dc135
XL
310 is_in_trait_impl: false,
311 is_in_dyn_type: false,
0531ce1d 312 anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
a1dfa0c6 313 type_def_lifetime_params: Default::default(),
e74abb32 314 current_module: hir::CRATE_HIR_ID,
ba9703b0 315 current_hir_id_owner: vec![(LocalDefId { local_def_index: CRATE_DEF_INDEX }, 0)],
a1dfa0c6 316 item_local_id_counters: Default::default(),
cc61c64b 317 node_id_to_hir_id: IndexVec::new(),
dc9dc135 318 generator_kind: None,
ba9703b0 319 task_context: None,
48663c56 320 current_item: None,
ff7c6d11
XL
321 lifetimes_to_define: Vec::new(),
322 is_collecting_in_band_lifetimes: false,
323 in_scope_lifetimes: Vec::new(),
dc9dc135
XL
324 allow_try_trait: Some([sym::try_trait][..].into()),
325 allow_gen_future: Some([sym::gen_future][..].into()),
dfeec247
XL
326 }
327 .lower_crate(krate)
a7813a04
XL
328}
329
8faf50e0 330#[derive(Copy, Clone, PartialEq)]
476ff2be
SL
331enum ParamMode {
332 /// Any path in a type context.
333 Explicit,
dc9dc135
XL
334 /// Path in a type definition, where the anonymous lifetime `'_` is not allowed.
335 ExplicitNamed,
476ff2be 336 /// The `module::Type` in `module::Type::method` in an expression.
0531ce1d 337 Optional,
476ff2be
SL
338}
339
3b2f2976
XL
340enum ParenthesizedGenericArgs {
341 Ok,
3b2f2976
XL
342 Err,
343}
344
0531ce1d 345/// What to do when we encounter an **anonymous** lifetime
9fa01778 346/// reference. Anonymous lifetime references come in two flavors. You
0531ce1d
XL
347/// have implicit, or fully elided, references to lifetimes, like the
348/// one in `&T` or `Ref<T>`, and you have `'_` lifetimes, like `&'_ T`
9fa01778 349/// or `Ref<'_, T>`. These often behave the same, but not always:
0531ce1d
XL
350///
351/// - certain usages of implicit references are deprecated, like
352/// `Ref<T>`, and we sometimes just give hard errors in those cases
353/// as well.
354/// - for object bounds there is a difference: `Box<dyn Foo>` is not
355/// the same as `Box<dyn Foo + '_>`.
356///
357/// We describe the effects of the various modes in terms of three cases:
358///
359/// - **Modern** -- includes all uses of `'_`, but also the lifetime arg
360/// of a `&` (e.g., the missing lifetime in something like `&T`)
361/// - **Dyn Bound** -- if you have something like `Box<dyn Foo>`,
362/// there is an elided lifetime bound (`Box<dyn Foo + 'X>`). These
363/// elided bounds follow special rules. Note that this only covers
364/// cases where *nothing* is written; the `'_` in `Box<dyn Foo +
365/// '_>` is a case of "modern" elision.
366/// - **Deprecated** -- this coverse cases like `Ref<T>`, where the lifetime
367/// parameter to ref is completely elided. `Ref<'_, T>` would be the modern,
368/// non-deprecated equivalent.
369///
370/// Currently, the handling of lifetime elision is somewhat spread out
371/// between HIR lowering and -- as described below -- the
372/// `resolve_lifetime` module. Often we "fallthrough" to that code by generating
373/// an "elided" or "underscore" lifetime name. In the future, we probably want to move
374/// everything into HIR lowering.
e1599b0c 375#[derive(Copy, Clone, Debug)]
0531ce1d
XL
376enum AnonymousLifetimeMode {
377 /// For **Modern** cases, create a new anonymous region parameter
378 /// and reference that.
379 ///
380 /// For **Dyn Bound** cases, pass responsibility to
381 /// `resolve_lifetime` code.
382 ///
383 /// For **Deprecated** cases, report an error.
384 CreateParameter,
385
0bf4aa26
XL
386 /// Give a hard error when either `&` or `'_` is written. Used to
387 /// rule out things like `where T: Foo<'_>`. Does not imply an
388 /// error on default object bounds (e.g., `Box<dyn Foo>`).
389 ReportError,
390
0531ce1d
XL
391 /// Pass responsibility to `resolve_lifetime` code for all cases.
392 PassThrough,
393}
394
dfeec247
XL
395struct ImplTraitTypeIdVisitor<'a> {
396 ids: &'a mut SmallVec<[NodeId; 1]>,
397}
0bf4aa26 398
dfeec247
XL
399impl Visitor<'_> for ImplTraitTypeIdVisitor<'_> {
400 fn visit_ty(&mut self, ty: &Ty) {
e74abb32 401 match ty.kind {
dfeec247 402 TyKind::Typeof(_) | TyKind::BareFn(_) => return,
0bf4aa26 403
532ac7d7 404 TyKind::ImplTrait(id, _) => self.ids.push(id),
dfeec247 405 _ => {}
0bf4aa26
XL
406 }
407 visit::walk_ty(self, ty);
408 }
409
dfeec247 410 fn visit_path_segment(&mut self, path_span: Span, path_segment: &PathSegment) {
0bf4aa26
XL
411 if let Some(ref p) = path_segment.args {
412 if let GenericArgs::Parenthesized(_) = **p {
413 return;
414 }
415 }
416 visit::walk_path_segment(self, path_span, path_segment)
417 }
418}
419
dfeec247
XL
420impl<'a, 'hir> LoweringContext<'a, 'hir> {
421 fn lower_crate(mut self, c: &Crate) -> hir::Crate<'hir> {
32a655c1
SL
422 /// Full-crate AST visitor that inserts into a fresh
423 /// `LoweringContext` any information that may be
0731742a
XL
424 /// needed from arbitrary locations in the crate,
425 /// e.g., the number of lifetime generic parameters
32a655c1 426 /// declared for every type and trait definition.
dfeec247
XL
427 struct MiscCollector<'tcx, 'lowering, 'hir> {
428 lctx: &'tcx mut LoweringContext<'lowering, 'hir>,
48663c56 429 hir_id_owner: Option<NodeId>,
32a655c1 430 }
476ff2be 431
dfeec247 432 impl MiscCollector<'_, '_, '_> {
ba9703b0 433 fn allocate_use_tree_hir_id_counters(&mut self, tree: &UseTree, owner: LocalDefId) {
532ac7d7
XL
434 match tree.kind {
435 UseTreeKind::Simple(_, id1, id2) => {
436 for &id in &[id1, id2] {
f035d41b 437 self.lctx.resolver.create_def(
532ac7d7
XL
438 owner,
439 id,
440 DefPathData::Misc,
416331ca 441 ExpnId::root(),
532ac7d7
XL
442 tree.prefix.span,
443 );
444 self.lctx.allocate_hir_id_counter(id);
445 }
446 }
447 UseTreeKind::Glob => (),
448 UseTreeKind::Nested(ref trees) => {
449 for &(ref use_tree, id) in trees {
48663c56 450 let hir_id = self.lctx.allocate_hir_id_counter(id);
532ac7d7
XL
451 self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
452 }
453 }
454 }
455 }
48663c56 456
dfeec247
XL
457 fn with_hir_id_owner<T>(
458 &mut self,
459 owner: Option<NodeId>,
460 f: impl FnOnce(&mut Self) -> T,
461 ) -> T {
48663c56
XL
462 let old = mem::replace(&mut self.hir_id_owner, owner);
463 let r = f(self);
464 self.hir_id_owner = old;
465 r
466 }
532ac7d7
XL
467 }
468
dfeec247 469 impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> {
dc9dc135 470 fn visit_pat(&mut self, p: &'tcx Pat) {
e74abb32 471 if let PatKind::Paren(..) | PatKind::Rest = p.kind {
48663c56 472 // Doesn't generate a HIR node
e1599b0c
XL
473 } else if let Some(owner) = self.hir_id_owner {
474 self.lctx.lower_node_id_with_owner(p.id, owner);
475 }
48663c56
XL
476
477 visit::walk_pat(self, p)
478 }
479
dc9dc135 480 fn visit_item(&mut self, item: &'tcx Item) {
48663c56 481 let hir_id = self.lctx.allocate_hir_id_counter(item.id);
cc61c64b 482
e74abb32 483 match item.kind {
0531ce1d
XL
484 ItemKind::Struct(_, ref generics)
485 | ItemKind::Union(_, ref generics)
486 | ItemKind::Enum(_, ref generics)
74b04a01 487 | ItemKind::TyAlias(_, ref generics, ..)
0531ce1d 488 | ItemKind::Trait(_, _, ref generics, ..) => {
f035d41b 489 let def_id = self.lctx.resolver.local_def_id(item.id);
0531ce1d
XL
490 let count = generics
491 .params
492 .iter()
8faf50e0
XL
493 .filter(|param| match param.kind {
494 ast::GenericParamKind::Lifetime { .. } => true,
495 _ => false,
496 })
ff7c6d11 497 .count();
ba9703b0 498 self.lctx.type_def_lifetime_params.insert(def_id.to_def_id(), count);
32a655c1 499 }
532ac7d7
XL
500 ItemKind::Use(ref use_tree) => {
501 self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
502 }
32a655c1
SL
503 _ => {}
504 }
48663c56
XL
505
506 self.with_hir_id_owner(Some(item.id), |this| {
507 visit::walk_item(this, item);
508 });
32a655c1 509 }
cc61c64b 510
74b04a01 511 fn visit_assoc_item(&mut self, item: &'tcx AssocItem, ctxt: AssocCtxt) {
532ac7d7 512 self.lctx.allocate_hir_id_counter(item.id);
74b04a01
XL
513 let owner = match (&item.kind, ctxt) {
514 // Ignore patterns in trait methods without bodies.
515 (AssocItemKind::Fn(_, _, _, None), AssocCtxt::Trait) => None,
516 _ => Some(item.id),
517 };
518 self.with_hir_id_owner(owner, |this| visit::walk_assoc_item(this, item, ctxt));
48663c56
XL
519 }
520
dc9dc135 521 fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) {
48663c56 522 // Ignore patterns in foreign items
dfeec247 523 self.with_hir_id_owner(None, |this| visit::walk_foreign_item(this, i));
48663c56
XL
524 }
525
dc9dc135 526 fn visit_ty(&mut self, t: &'tcx Ty) {
e74abb32 527 match t.kind {
48663c56
XL
528 // Mirrors the case in visit::walk_ty
529 TyKind::BareFn(ref f) => {
dfeec247 530 walk_list!(self, visit_generic_param, &f.generic_params);
48663c56 531 // Mirrors visit::walk_fn_decl
e1599b0c 532 for parameter in &f.decl.inputs {
48663c56
XL
533 // We don't lower the ids of argument patterns
534 self.with_hir_id_owner(None, |this| {
e1599b0c 535 this.visit_pat(&parameter.pat);
48663c56 536 });
e1599b0c 537 self.visit_ty(&parameter.ty)
48663c56
XL
538 }
539 self.visit_fn_ret_ty(&f.decl.output)
540 }
541 _ => visit::walk_ty(self, t),
542 }
cc61c64b 543 }
476ff2be 544 }
476ff2be 545
cc61c64b 546 self.lower_node_id(CRATE_NODE_ID);
ba9703b0 547 debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == Some(hir::CRATE_HIR_ID));
cc61c64b 548
48663c56 549 visit::walk_crate(&mut MiscCollector { lctx: &mut self, hir_id_owner: None }, c);
416331ca 550 visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
32a655c1
SL
551
552 let module = self.lower_mod(&c.module);
553 let attrs = self.lower_attrs(&c.attrs);
8bb4bdeb 554 let body_ids = body_ids(&self.bodies);
ba9703b0
XL
555 let proc_macros =
556 c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
32a655c1 557
f035d41b
XL
558 let trait_map = self
559 .resolver
560 .trait_map()
561 .iter()
3dfed10e
XL
562 .filter_map(|(&k, v)| {
563 self.node_id_to_hir_id.get(k).and_then(|id| id.as_ref()).map(|id| (*id, v.clone()))
564 })
f035d41b
XL
565 .collect();
566
567 let mut def_id_to_hir_id = IndexVec::default();
568
569 for (node_id, hir_id) in self.node_id_to_hir_id.into_iter_enumerated() {
570 if let Some(def_id) = self.resolver.opt_local_def_id(node_id) {
571 if def_id_to_hir_id.len() <= def_id.index() {
572 def_id_to_hir_id.resize(def_id.index() + 1, None);
573 }
574 def_id_to_hir_id[def_id] = hir_id;
575 }
576 }
577
578 self.resolver.definitions().init_def_id_to_hir_id_mapping(def_id_to_hir_id);
cc61c64b 579
32a655c1 580 hir::Crate {
ba9703b0 581 item: hir::CrateItem { module, attrs, span: c.span },
dfeec247
XL
582 exported_macros: self.arena.alloc_from_iter(self.exported_macros),
583 non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
32a655c1
SL
584 items: self.items,
585 trait_items: self.trait_items,
586 impl_items: self.impl_items,
587 bodies: self.bodies,
041b39d2 588 body_ids,
8bb4bdeb 589 trait_impls: self.trait_impls,
0731742a 590 modules: self.modules,
74b04a01 591 proc_macros,
f035d41b 592 trait_map,
32a655c1 593 }
476ff2be 594 }
a7813a04 595
dfeec247 596 fn insert_item(&mut self, item: hir::Item<'hir>) {
532ac7d7 597 let id = item.hir_id;
dc9dc135 598 // FIXME: Use `debug_asset-rt`.
532ac7d7 599 assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
0731742a
XL
600 self.items.insert(id, item);
601 self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
602 }
603
48663c56 604 fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
dc9dc135 605 // Set up the counter if needed.
532ac7d7 606 self.item_local_id_counters.entry(owner).or_insert(0);
0731742a 607 // Always allocate the first `HirId` for the owner itself.
532ac7d7 608 let lowered = self.lower_node_id_with_owner(owner, owner);
48663c56 609 debug_assert_eq!(lowered.local_id.as_u32(), 0);
532ac7d7 610 lowered
cc61c64b
XL
611 }
612
dfeec247
XL
613 fn lower_node_id_generic(
614 &mut self,
615 ast_node_id: NodeId,
616 alloc_hir_id: impl FnOnce(&mut Self) -> hir::HirId,
617 ) -> hir::HirId {
ba9703b0 618 assert_ne!(ast_node_id, DUMMY_NODE_ID);
cc61c64b
XL
619
620 let min_size = ast_node_id.as_usize() + 1;
621
622 if min_size > self.node_id_to_hir_id.len() {
ba9703b0 623 self.node_id_to_hir_id.resize(min_size, None);
cc61c64b
XL
624 }
625
ba9703b0
XL
626 if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] {
627 existing_hir_id
628 } else {
0731742a 629 // Generate a new `HirId`.
3b2f2976 630 let hir_id = alloc_hir_id(self);
ba9703b0 631 self.node_id_to_hir_id[ast_node_id] = Some(hir_id);
48663c56
XL
632
633 hir_id
cc61c64b 634 }
cc61c64b
XL
635 }
636
dfeec247
XL
637 fn with_hir_id_owner<T>(&mut self, owner: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
638 let counter = self
639 .item_local_id_counters
0531ce1d 640 .insert(owner, HIR_ID_COUNTER_LOCKED)
dc9dc135 641 .unwrap_or_else(|| panic!("no `item_local_id_counters` entry for {:?}", owner));
f035d41b 642 let def_id = self.resolver.local_def_id(owner);
ba9703b0 643 self.current_hir_id_owner.push((def_id, counter));
94b46f34 644 let ret = f(self);
ba9703b0 645 let (new_def_id, new_counter) = self.current_hir_id_owner.pop().unwrap();
cc61c64b 646
ba9703b0 647 debug_assert!(def_id == new_def_id);
cc61c64b
XL
648 debug_assert!(new_counter >= counter);
649
dfeec247 650 let prev = self.item_local_id_counters.insert(owner, new_counter).unwrap();
cc61c64b 651 debug_assert!(prev == HIR_ID_COUNTER_LOCKED);
94b46f34 652 ret
cc61c64b
XL
653 }
654
0731742a
XL
655 /// This method allocates a new `HirId` for the given `NodeId` and stores it in
656 /// the `LoweringContext`'s `NodeId => HirId` map.
657 /// Take care not to call this method if the resulting `HirId` is then not
cc61c64b 658 /// actually used in the HIR, as that would trigger an assertion in the
0731742a
XL
659 /// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
660 /// properly. Calling the method twice with the same `NodeId` is fine though.
48663c56 661 fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
cc61c64b 662 self.lower_node_id_generic(ast_node_id, |this| {
ba9703b0 663 let &mut (owner, ref mut local_id_counter) =
0531ce1d 664 this.current_hir_id_owner.last_mut().unwrap();
cc61c64b
XL
665 let local_id = *local_id_counter;
666 *local_id_counter += 1;
ba9703b0 667 hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) }
cc61c64b
XL
668 })
669 }
670
48663c56 671 fn lower_node_id_with_owner(&mut self, ast_node_id: NodeId, owner: NodeId) -> hir::HirId {
cc61c64b 672 self.lower_node_id_generic(ast_node_id, |this| {
94b46f34
XL
673 let local_id_counter = this
674 .item_local_id_counters
675 .get_mut(&owner)
dc9dc135 676 .expect("called `lower_node_id_with_owner` before `allocate_hir_id_counter`");
cc61c64b
XL
677 let local_id = *local_id_counter;
678
679 // We want to be sure not to modify the counter in the map while it
680 // is also on the stack. Otherwise we'll get lost updates when writing
681 // back from the stack to the map.
682 debug_assert!(local_id != HIR_ID_COUNTER_LOCKED);
683
684 *local_id_counter += 1;
f035d41b
XL
685 let owner = this.resolver.opt_local_def_id(owner).expect(
686 "you forgot to call `create_def` or are lowering node-IDs \
ba9703b0 687 that do not belong to the current owner",
dfeec247
XL
688 );
689
ba9703b0 690 hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) }
cc61c64b
XL
691 })
692 }
693
48663c56 694 fn next_id(&mut self) -> hir::HirId {
60c5eb7d
XL
695 let node_id = self.resolver.next_node_id();
696 self.lower_node_id(node_id)
3157f602
XL
697 }
698
48663c56
XL
699 fn lower_res(&mut self, res: Res<NodeId>) -> Res {
700 res.map_id(|id| {
701 self.lower_node_id_generic(id, |_| {
e1599b0c 702 panic!("expected `NodeId` to be lowered already for res {:#?}", res);
48663c56 703 })
476ff2be
SL
704 })
705 }
706
48663c56
XL
707 fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
708 self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
94b46f34 709 if pr.unresolved_segments() != 0 {
ba9703b0 710 panic!("path not fully resolved: {:?}", pr);
94b46f34 711 }
48663c56 712 pr.base_res()
94b46f34
XL
713 })
714 }
715
48663c56
XL
716 fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
717 self.resolver.get_import_res(id).present_items()
54a0048b 718 }
e9174d1e 719
dfeec247 720 fn diagnostic(&self) -> &rustc_errors::Handler {
48663c56 721 self.sess.diagnostic()
a7813a04 722 }
7453a54e 723
dc9dc135
XL
724 /// Reuses the span but adds information like the kind of the desugaring and features that are
725 /// allowed inside this span.
726 fn mark_span_with_reason(
727 &self,
416331ca 728 reason: DesugaringKind,
dc9dc135
XL
729 span: Span,
730 allow_internal_unstable: Option<Lrc<[Symbol]>>,
731 ) -> Span {
e1599b0c 732 span.fresh_expansion(ExpnData {
dc9dc135 733 allow_internal_unstable,
f9f354fc 734 ..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition(), None)
416331ca 735 })
dc9dc135
XL
736 }
737
94b46f34
XL
738 fn with_anonymous_lifetime_mode<R>(
739 &mut self,
740 anonymous_lifetime_mode: AnonymousLifetimeMode,
741 op: impl FnOnce(&mut Self) -> R,
742 ) -> R {
e1599b0c
XL
743 debug!(
744 "with_anonymous_lifetime_mode(anonymous_lifetime_mode={:?})",
745 anonymous_lifetime_mode,
746 );
94b46f34
XL
747 let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
748 self.anonymous_lifetime_mode = anonymous_lifetime_mode;
749 let result = op(self);
750 self.anonymous_lifetime_mode = old_anonymous_lifetime_mode;
dfeec247
XL
751 debug!(
752 "with_anonymous_lifetime_mode: restoring anonymous_lifetime_mode={:?}",
753 old_anonymous_lifetime_mode
754 );
94b46f34
XL
755 result
756 }
757
dc9dc135 758 /// Creates a new `hir::GenericParam` for every new lifetime and
0531ce1d
XL
759 /// type parameter encountered while evaluating `f`. Definitions
760 /// are created with the parent provided. If no `parent_id` is
761 /// provided, no definitions will be returned.
762 ///
763 /// Presuming that in-band lifetimes are enabled, then
764 /// `self.anonymous_lifetime_mode` will be updated to match the
e1599b0c 765 /// parameter while `f` is running (and restored afterwards).
dfeec247 766 fn collect_in_band_defs<T>(
ff7c6d11 767 &mut self,
ba9703b0 768 parent_def_id: LocalDefId,
0531ce1d 769 anonymous_lifetime_mode: AnonymousLifetimeMode,
dfeec247
XL
770 f: impl FnOnce(&mut Self) -> (Vec<hir::GenericParam<'hir>>, T),
771 ) -> (Vec<hir::GenericParam<'hir>>, T) {
ff7c6d11
XL
772 assert!(!self.is_collecting_in_band_lifetimes);
773 assert!(self.lifetimes_to_define.is_empty());
0531ce1d 774 let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
ff7c6d11 775
0bf4aa26
XL
776 self.anonymous_lifetime_mode = anonymous_lifetime_mode;
777 self.is_collecting_in_band_lifetimes = true;
ff7c6d11 778
8faf50e0 779 let (in_band_ty_params, res) = f(self);
ff7c6d11
XL
780
781 self.is_collecting_in_band_lifetimes = false;
0531ce1d 782 self.anonymous_lifetime_mode = old_anonymous_lifetime_mode;
ff7c6d11 783
ff7c6d11
XL
784 let lifetimes_to_define = self.lifetimes_to_define.split_off(0);
785
0531ce1d
XL
786 let params = lifetimes_to_define
787 .into_iter()
ba9703b0 788 .map(|(span, hir_name)| self.lifetime_to_generic_param(span, hir_name, parent_def_id))
8faf50e0 789 .chain(in_band_ty_params.into_iter())
0531ce1d 790 .collect();
ff7c6d11
XL
791
792 (params, res)
793 }
794
532ac7d7
XL
795 /// Converts a lifetime into a new generic parameter.
796 fn lifetime_to_generic_param(
797 &mut self,
798 span: Span,
799 hir_name: ParamName,
ba9703b0 800 parent_def_id: LocalDefId,
dfeec247 801 ) -> hir::GenericParam<'hir> {
60c5eb7d 802 let node_id = self.resolver.next_node_id();
532ac7d7
XL
803
804 // Get the name we'll use to make the def-path. Note
805 // that collisions are ok here and this shouldn't
806 // really show up for end-user.
807 let (str_name, kind) = match hir_name {
dfeec247
XL
808 ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::InBand),
809 ParamName::Fresh(_) => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Elided),
810 ParamName::Error => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Error),
532ac7d7
XL
811 };
812
813 // Add a definition for the in-band lifetime def.
f035d41b 814 self.resolver.create_def(
ba9703b0 815 parent_def_id,
532ac7d7 816 node_id,
48663c56 817 DefPathData::LifetimeNs(str_name),
416331ca 818 ExpnId::root(),
532ac7d7
XL
819 span,
820 );
821
822 hir::GenericParam {
48663c56 823 hir_id: self.lower_node_id(node_id),
532ac7d7 824 name: hir_name,
dfeec247
XL
825 attrs: &[],
826 bounds: &[],
532ac7d7
XL
827 span,
828 pure_wrt_drop: false,
dfeec247 829 kind: hir::GenericParamKind::Lifetime { kind },
532ac7d7
XL
830 }
831 }
832
0531ce1d
XL
833 /// When there is a reference to some lifetime `'a`, and in-band
834 /// lifetimes are enabled, then we want to push that lifetime into
835 /// the vector of names to define later. In that case, it will get
836 /// added to the appropriate generics.
8faf50e0 837 fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
0531ce1d
XL
838 if !self.is_collecting_in_band_lifetimes {
839 return;
840 }
841
b7449926
XL
842 if !self.sess.features_untracked().in_band_lifetimes {
843 return;
844 }
845
ba9703b0 846 if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.normalize_to_macros_2_0())) {
0531ce1d
XL
847 return;
848 }
849
8faf50e0 850 let hir_name = ParamName::Plain(ident);
0531ce1d 851
ba9703b0
XL
852 if self.lifetimes_to_define.iter().any(|(_, lt_name)| {
853 lt_name.normalize_to_macros_2_0() == hir_name.normalize_to_macros_2_0()
854 }) {
0531ce1d
XL
855 return;
856 }
857
8faf50e0 858 self.lifetimes_to_define.push((ident.span, hir_name));
0531ce1d
XL
859 }
860
861 /// When we have either an elided or `'_` lifetime in an impl
0bf4aa26 862 /// header, we convert it to an in-band lifetime.
8faf50e0 863 fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName {
0531ce1d 864 assert!(self.is_collecting_in_band_lifetimes);
e1599b0c 865 let index = self.lifetimes_to_define.len() + self.in_scope_lifetimes.len();
8faf50e0 866 let hir_name = ParamName::Fresh(index);
0531ce1d
XL
867 self.lifetimes_to_define.push((span, hir_name));
868 hir_name
869 }
870
8faf50e0 871 // Evaluates `f` with the lifetimes in `params` in-scope.
ff7c6d11
XL
872 // This is used to track which lifetimes have already been defined, and
873 // which are new in-band lifetimes that need to have a definition created
874 // for them.
dfeec247
XL
875 fn with_in_scope_lifetime_defs<T>(
876 &mut self,
877 params: &[GenericParam],
878 f: impl FnOnce(&mut Self) -> T,
879 ) -> T {
ff7c6d11 880 let old_len = self.in_scope_lifetimes.len();
8faf50e0 881 let lt_def_names = params.iter().filter_map(|param| match param.kind {
ba9703b0
XL
882 GenericParamKind::Lifetime { .. } => {
883 Some(ParamName::Plain(param.ident.normalize_to_macros_2_0()))
884 }
8faf50e0
XL
885 _ => None,
886 });
ff7c6d11
XL
887 self.in_scope_lifetimes.extend(lt_def_names);
888
889 let res = f(self);
890
891 self.in_scope_lifetimes.truncate(old_len);
892 res
893 }
894
0531ce1d
XL
895 /// Appends in-band lifetime defs and argument-position `impl
896 /// Trait` defs to the existing set of generics.
897 ///
898 /// Presuming that in-band lifetimes are enabled, then
899 /// `self.anonymous_lifetime_mode` will be updated to match the
e1599b0c 900 /// parameter while `f` is running (and restored afterwards).
dfeec247 901 fn add_in_band_defs<T>(
ff7c6d11
XL
902 &mut self,
903 generics: &Generics,
ba9703b0 904 parent_def_id: LocalDefId,
0531ce1d 905 anonymous_lifetime_mode: AnonymousLifetimeMode,
dfeec247
XL
906 f: impl FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
907 ) -> (hir::Generics<'hir>, T) {
908 let (in_band_defs, (mut lowered_generics, res)) =
909 self.with_in_scope_lifetime_defs(&generics.params, |this| {
ba9703b0 910 this.collect_in_band_defs(parent_def_id, anonymous_lifetime_mode, |this| {
8faf50e0 911 let mut params = Vec::new();
532ac7d7
XL
912 // Note: it is necessary to lower generics *before* calling `f`.
913 // When lowering `async fn`, there's a final step when lowering
914 // the return type that assumes that all in-scope lifetimes have
915 // already been added to either `in_scope_lifetimes` or
916 // `lifetimes_to_define`. If we swapped the order of these two,
917 // in-band-lifetimes introduced by generics or where-clauses
918 // wouldn't have been added yet.
dfeec247
XL
919 let generics =
920 this.lower_generics_mut(generics, ImplTraitContext::Universal(&mut params));
8faf50e0
XL
921 let res = f(this, &mut params);
922 (params, (generics, res))
0531ce1d 923 })
dfeec247 924 });
ff7c6d11 925
3dfed10e 926 lowered_generics.params.extend(in_band_defs);
8bb4bdeb 927
dfeec247 928 let lowered_generics = lowered_generics.into_generics(self.arena);
416331ca 929 (lowered_generics, res)
8bb4bdeb
XL
930 }
931
dfeec247 932 fn with_dyn_type_scope<T>(&mut self, in_scope: bool, f: impl FnOnce(&mut Self) -> T) -> T {
dc9dc135
XL
933 let was_in_dyn_type = self.is_in_dyn_type;
934 self.is_in_dyn_type = in_scope;
935
936 let result = f(self);
937
938 self.is_in_dyn_type = was_in_dyn_type;
939
940 result
941 }
942
dfeec247 943 fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
8bb4bdeb
XL
944 let was_in_loop_condition = self.is_in_loop_condition;
945 self.is_in_loop_condition = false;
946
416331ca
XL
947 let catch_scopes = mem::take(&mut self.catch_scopes);
948 let loop_scopes = mem::take(&mut self.loop_scopes);
0bf4aa26 949 let ret = f(self);
cc61c64b
XL
950 self.catch_scopes = catch_scopes;
951 self.loop_scopes = loop_scopes;
8bb4bdeb
XL
952
953 self.is_in_loop_condition = was_in_loop_condition;
954
0bf4aa26 955 ret
8bb4bdeb
XL
956 }
957
dfeec247
XL
958 fn lower_attrs(&mut self, attrs: &[Attribute]) -> &'hir [Attribute] {
959 self.arena.alloc_from_iter(attrs.iter().map(|a| self.lower_attr(a)))
416331ca
XL
960 }
961
ea8adc8c 962 fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
13cf67c4
XL
963 // Note that we explicitly do not walk the path. Since we don't really
964 // lower attributes (we use the AST version) there is nowhere to keep
0731742a 965 // the `HirId`s. We don't actually need HIR version of attributes anyway.
60c5eb7d 966 let kind = match attr.kind {
dfeec247
XL
967 AttrKind::Normal(ref item) => AttrKind::Normal(AttrItem {
968 path: item.path.clone(),
969 args: self.lower_mac_args(&item.args),
1b1a35ee 970 tokens: None,
dfeec247 971 }),
3dfed10e 972 AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
60c5eb7d
XL
973 };
974
dfeec247 975 Attribute { kind, id: attr.id, style: attr.style, span: attr.span }
ea8adc8c
XL
976 }
977
60c5eb7d
XL
978 fn lower_mac_args(&mut self, args: &MacArgs) -> MacArgs {
979 match *args {
980 MacArgs::Empty => MacArgs::Empty,
dfeec247
XL
981 MacArgs::Delimited(dspan, delim, ref tokens) => {
982 MacArgs::Delimited(dspan, delim, self.lower_token_stream(tokens.clone()))
983 }
984 MacArgs::Eq(eq_span, ref tokens) => {
985 MacArgs::Eq(eq_span, self.lower_token_stream(tokens.clone()))
986 }
60c5eb7d
XL
987 }
988 }
989
ea8adc8c 990 fn lower_token_stream(&mut self, tokens: TokenStream) -> TokenStream {
dfeec247 991 tokens.into_trees().flat_map(|tree| self.lower_token_tree(tree).into_trees()).collect()
ea8adc8c
XL
992 }
993
994 fn lower_token_tree(&mut self, tree: TokenTree) -> TokenStream {
995 match tree {
dc9dc135 996 TokenTree::Token(token) => self.lower_token(token),
dfeec247
XL
997 TokenTree::Delimited(span, delim, tts) => {
998 TokenTree::Delimited(span, delim, self.lower_token_stream(tts)).into()
999 }
ea8adc8c
XL
1000 }
1001 }
1002
dc9dc135
XL
1003 fn lower_token(&mut self, token: Token) -> TokenStream {
1004 match token.kind {
1005 token::Interpolated(nt) => {
e74abb32 1006 let tts = (self.nt_to_tokenstream)(&nt, &self.sess.parse_sess, token.span);
f035d41b
XL
1007 TokenTree::Delimited(
1008 DelimSpan::from_single(token.span),
1009 DelimToken::NoDelim,
1010 self.lower_token_stream(tts),
1011 )
1012 .into()
9fa01778 1013 }
dc9dc135 1014 _ => TokenTree::Token(token).into(),
ea8adc8c 1015 }
7453a54e 1016 }
7453a54e 1017
dc9dc135
XL
1018 /// Given an associated type constraint like one of these:
1019 ///
1020 /// ```
1021 /// T: Iterator<Item: Debug>
1022 /// ^^^^^^^^^^^
1023 /// T: Iterator<Item = Debug>
1024 /// ^^^^^^^^^^^^
1025 /// ```
1026 ///
1027 /// returns a `hir::TypeBinding` representing `Item`.
e1599b0c
XL
1028 fn lower_assoc_ty_constraint(
1029 &mut self,
1030 constraint: &AssocTyConstraint,
dfeec247
XL
1031 itctx: ImplTraitContext<'_, 'hir>,
1032 ) -> hir::TypeBinding<'hir> {
e1599b0c 1033 debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
dc9dc135 1034
e1599b0c 1035 let kind = match constraint.kind {
dfeec247
XL
1036 AssocTyConstraintKind::Equality { ref ty } => {
1037 hir::TypeBindingKind::Equality { ty: self.lower_ty(ty, itctx) }
1038 }
dc9dc135 1039 AssocTyConstraintKind::Bound { ref bounds } => {
f035d41b 1040 let mut capturable_lifetimes;
dc9dc135
XL
1041 // Piggy-back on the `impl Trait` context to figure out the correct behavior.
1042 let (desugar_to_impl_trait, itctx) = match itctx {
1043 // We are in the return position:
1044 //
1045 // fn foo() -> impl Iterator<Item: Debug>
1046 //
1047 // so desugar to
1048 //
1049 // fn foo() -> impl Iterator<Item = impl Debug>
f035d41b
XL
1050 ImplTraitContext::ReturnPositionOpaqueTy { .. }
1051 | ImplTraitContext::OtherOpaqueTy { .. } => (true, itctx),
dc9dc135
XL
1052
1053 // We are in the argument position, but within a dyn type:
1054 //
1055 // fn foo(x: dyn Iterator<Item: Debug>)
1056 //
1057 // so desugar to
1058 //
1059 // fn foo(x: dyn Iterator<Item = impl Debug>)
74b04a01 1060 ImplTraitContext::Universal(..) if self.is_in_dyn_type => (true, itctx),
dc9dc135
XL
1061
1062 // In `type Foo = dyn Iterator<Item: Debug>` we desugar to
1063 // `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
1064 // "impl trait context" to permit `impl Debug` in this position (it desugars
416331ca 1065 // then to an opaque type).
dc9dc135
XL
1066 //
1067 // FIXME: this is only needed until `impl Trait` is allowed in type aliases.
dfeec247 1068 ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => {
f035d41b
XL
1069 capturable_lifetimes = FxHashSet::default();
1070 (
1071 true,
1072 ImplTraitContext::OtherOpaqueTy {
1073 capturable_lifetimes: &mut capturable_lifetimes,
1074 origin: hir::OpaqueTyOrigin::Misc,
1075 },
1076 )
dfeec247 1077 }
dc9dc135 1078
e1599b0c 1079 // We are in the parameter position, but not within a dyn type:
dc9dc135
XL
1080 //
1081 // fn foo(x: impl Iterator<Item: Debug>)
1082 //
1083 // so we leave it as is and this gets expanded in astconv to a bound like
1084 // `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
1085 // `impl Iterator`.
1086 _ => (false, itctx),
1087 };
1088
1089 if desugar_to_impl_trait {
1090 // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1091 // constructing the HIR for `impl bounds...` and then lowering that.
1092
60c5eb7d 1093 let impl_trait_node_id = self.resolver.next_node_id();
ba9703b0 1094 let parent_def_id = self.current_hir_id_owner.last().unwrap().0;
f035d41b 1095 self.resolver.create_def(
ba9703b0 1096 parent_def_id,
dc9dc135
XL
1097 impl_trait_node_id,
1098 DefPathData::ImplTrait,
416331ca 1099 ExpnId::root(),
e1599b0c 1100 constraint.span,
dc9dc135
XL
1101 );
1102
1103 self.with_dyn_type_scope(false, |this| {
60c5eb7d 1104 let node_id = this.resolver.next_node_id();
dc9dc135
XL
1105 let ty = this.lower_ty(
1106 &Ty {
60c5eb7d 1107 id: node_id,
e74abb32 1108 kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
e1599b0c 1109 span: constraint.span,
1b1a35ee 1110 tokens: None,
dc9dc135
XL
1111 },
1112 itctx,
1113 );
1114
dfeec247 1115 hir::TypeBindingKind::Equality { ty }
dc9dc135
XL
1116 })
1117 } else {
1118 // Desugar `AssocTy: Bounds` into a type binding where the
1119 // later desugars into a trait predicate.
1120 let bounds = self.lower_param_bounds(bounds, itctx);
1121
dfeec247 1122 hir::TypeBindingKind::Constraint { bounds }
dc9dc135
XL
1123 }
1124 }
1125 };
1126
a7813a04 1127 hir::TypeBinding {
e1599b0c
XL
1128 hir_id: self.lower_node_id(constraint.id),
1129 ident: constraint.ident,
dc9dc135 1130 kind,
e1599b0c 1131 span: constraint.span,
a7813a04 1132 }
92a42be0 1133 }
e9174d1e 1134
60c5eb7d
XL
1135 fn lower_generic_arg(
1136 &mut self,
1137 arg: &ast::GenericArg,
dfeec247
XL
1138 itctx: ImplTraitContext<'_, 'hir>,
1139 ) -> hir::GenericArg<'hir> {
8faf50e0
XL
1140 match arg {
1141 ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
60c5eb7d 1142 ast::GenericArg::Type(ty) => {
74b04a01 1143 // We parse const arguments as path types as we cannot distinguish them during
60c5eb7d
XL
1144 // parsing. We try to resolve that ambiguity by attempting resolution in both the
1145 // type and value namespaces. If we resolved the path in the value namespace, we
1146 // transform it into a generic const argument.
1147 if let TyKind::Path(ref qself, ref path) = ty.kind {
1148 if let Some(partial_res) = self.resolver.get_partial_res(ty.id) {
1149 let res = partial_res.base_res();
1150 if !res.matches_ns(Namespace::TypeNS) {
1151 debug!(
1152 "lower_generic_arg: Lowering type argument as const argument: {:?}",
1153 ty,
1154 );
1155
1156 // Construct a AnonConst where the expr is the "ty"'s path.
1157
ba9703b0 1158 let parent_def_id = self.current_hir_id_owner.last().unwrap().0;
60c5eb7d
XL
1159 let node_id = self.resolver.next_node_id();
1160
1161 // Add a definition for the in-band const def.
f035d41b 1162 self.resolver.create_def(
ba9703b0 1163 parent_def_id,
60c5eb7d
XL
1164 node_id,
1165 DefPathData::AnonConst,
1166 ExpnId::root(),
1167 ty.span,
1168 );
1169
1170 let path_expr = Expr {
1171 id: ty.id,
1172 kind: ExprKind::Path(qself.clone(), path.clone()),
1173 span: ty.span,
dfeec247 1174 attrs: AttrVec::new(),
f9f354fc 1175 tokens: None,
60c5eb7d
XL
1176 };
1177
dfeec247
XL
1178 let ct = self.with_new_scopes(|this| hir::AnonConst {
1179 hir_id: this.lower_node_id(node_id),
1180 body: this.lower_const_body(path_expr.span, Some(&path_expr)),
60c5eb7d 1181 });
dfeec247 1182 return GenericArg::Const(ConstArg { value: ct, span: ty.span });
60c5eb7d
XL
1183 }
1184 }
1185 }
1186 GenericArg::Type(self.lower_ty_direct(&ty, itctx))
1187 }
dfeec247
XL
1188 ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1189 value: self.lower_anon_const(&ct),
1190 span: ct.value.span,
1191 }),
8faf50e0
XL
1192 }
1193 }
1194
dfeec247
XL
1195 fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_, 'hir>) -> &'hir hir::Ty<'hir> {
1196 self.arena.alloc(self.lower_ty_direct(t, itctx))
8faf50e0
XL
1197 }
1198
dc9dc135
XL
1199 fn lower_path_ty(
1200 &mut self,
1201 t: &Ty,
1202 qself: &Option<QSelf>,
1203 path: &Path,
1204 param_mode: ParamMode,
dfeec247
XL
1205 itctx: ImplTraitContext<'_, 'hir>,
1206 ) -> hir::Ty<'hir> {
dc9dc135
XL
1207 let id = self.lower_node_id(t.id);
1208 let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
1209 let ty = self.ty_path(id, t.span, qpath);
e74abb32 1210 if let hir::TyKind::TraitObject(..) = ty.kind {
dc9dc135
XL
1211 self.maybe_lint_bare_trait(t.span, t.id, qself.is_none() && path.is_global());
1212 }
1213 ty
1214 }
1215
dfeec247
XL
1216 fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1217 hir::Ty { hir_id: self.next_id(), kind, span }
1218 }
1219
1220 fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
1221 self.ty(span, hir::TyKind::Tup(tys))
1222 }
1223
1224 fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_, 'hir>) -> hir::Ty<'hir> {
e74abb32 1225 let kind = match t.kind {
8faf50e0
XL
1226 TyKind::Infer => hir::TyKind::Infer,
1227 TyKind::Err => hir::TyKind::Err,
1228 TyKind::Slice(ref ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
1229 TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
cc61c64b 1230 TyKind::Rptr(ref region, ref mt) => {
0bf4aa26 1231 let span = self.sess.source_map().next_point(t.span.shrink_to_lo());
cc61c64b
XL
1232 let lifetime = match *region {
1233 Some(ref lt) => self.lower_lifetime(lt),
0531ce1d 1234 None => self.elided_ref_lifetime(span),
cc61c64b 1235 };
8faf50e0 1236 hir::TyKind::Rptr(lifetime, self.lower_mt(mt, itctx))
cc61c64b 1237 }
dfeec247
XL
1238 TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(&f.generic_params, |this| {
1239 this.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
1240 hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
1241 generic_params: this.lower_generic_params(
1242 &f.generic_params,
1243 &NodeMap::default(),
1244 ImplTraitContext::disallowed(),
1245 ),
74b04a01 1246 unsafety: this.lower_unsafety(f.unsafety),
dfeec247
XL
1247 abi: this.lower_extern(f.ext),
1248 decl: this.lower_fn_decl(&f.decl, None, false, None),
1249 param_names: this.lower_fn_params_to_names(&f.decl),
1250 }))
1251 })
1252 }),
8faf50e0 1253 TyKind::Never => hir::TyKind::Never,
cc61c64b 1254 TyKind::Tup(ref tys) => {
dfeec247
XL
1255 hir::TyKind::Tup(self.arena.alloc_from_iter(
1256 tys.iter().map(|ty| self.lower_ty_direct(ty, itctx.reborrow())),
1257 ))
cc61c64b
XL
1258 }
1259 TyKind::Paren(ref ty) => {
8faf50e0 1260 return self.lower_ty_direct(ty, itctx);
cc61c64b
XL
1261 }
1262 TyKind::Path(ref qself, ref path) => {
dc9dc135 1263 return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
cc61c64b 1264 }
48663c56
XL
1265 TyKind::ImplicitSelf => {
1266 let res = self.expect_full_res(t.id);
1267 let res = self.lower_res(res);
1268 hir::TyKind::Path(hir::QPath::Resolved(
1269 None,
dfeec247 1270 self.arena.alloc(hir::Path {
48663c56 1271 res,
dfeec247 1272 segments: arena_vec![self; hir::PathSegment::from_ident(
e1599b0c 1273 Ident::with_dummy_span(kw::SelfUpper)
48663c56
XL
1274 )],
1275 span: t.span,
1276 }),
1277 ))
dfeec247 1278 }
cc61c64b 1279 TyKind::Array(ref ty, ref length) => {
8faf50e0 1280 hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_anon_const(length))
cc61c64b 1281 }
dfeec247 1282 TyKind::Typeof(ref expr) => hir::TyKind::Typeof(self.lower_anon_const(expr)),
0531ce1d 1283 TyKind::TraitObject(ref bounds, kind) => {
cc61c64b 1284 let mut lifetime_bound = None;
dc9dc135 1285 let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
dfeec247
XL
1286 let bounds =
1287 this.arena.alloc_from_iter(bounds.iter().filter_map(
1288 |bound| match *bound {
ba9703b0
XL
1289 GenericBound::Trait(
1290 ref ty,
1291 TraitBoundModifier::None | TraitBoundModifier::MaybeConst,
1292 ) => Some(this.lower_poly_trait_ref(ty, itctx.reborrow())),
dfeec247
XL
1293 // `?const ?Bound` will cause an error during AST validation
1294 // anyways, so treat it like `?Bound` as compilation proceeds.
ba9703b0
XL
1295 GenericBound::Trait(
1296 _,
1297 TraitBoundModifier::Maybe | TraitBoundModifier::MaybeConstMaybe,
1298 ) => None,
dfeec247
XL
1299 GenericBound::Outlives(ref lifetime) => {
1300 if lifetime_bound.is_none() {
1301 lifetime_bound = Some(this.lower_lifetime(lifetime));
1302 }
1303 None
1304 }
1305 },
1306 ));
dc9dc135
XL
1307 let lifetime_bound =
1308 lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
1309 (bounds, lifetime_bound)
1310 });
0531ce1d
XL
1311 if kind != TraitObjectSyntax::Dyn {
1312 self.maybe_lint_bare_trait(t.span, t.id, false);
1313 }
8faf50e0 1314 hir::TyKind::TraitObject(bounds, lifetime_bound)
cc61c64b 1315 }
8faf50e0 1316 TyKind::ImplTrait(def_node_id, ref bounds) => {
ff7c6d11 1317 let span = t.span;
abe05a73 1318 match itctx {
f035d41b
XL
1319 ImplTraitContext::ReturnPositionOpaqueTy { fn_def_id, origin } => self
1320 .lower_opaque_impl_trait(
1321 span,
1322 Some(fn_def_id),
1323 origin,
1324 def_node_id,
1325 None,
1326 |this| this.lower_param_bounds(bounds, itctx),
1327 ),
1328 ImplTraitContext::OtherOpaqueTy { ref capturable_lifetimes, origin } => {
1329 // Reset capturable lifetimes, any nested impl trait
1330 // types will inherit lifetimes from this opaque type,
1331 // so don't need to capture them again.
1332 let nested_itctx = ImplTraitContext::OtherOpaqueTy {
1333 capturable_lifetimes: &mut FxHashSet::default(),
1334 origin,
1335 };
1336 self.lower_opaque_impl_trait(
1337 span,
1338 None,
1339 origin,
1340 def_node_id,
1341 Some(capturable_lifetimes),
1342 |this| this.lower_param_bounds(bounds, nested_itctx),
1343 )
8faf50e0
XL
1344 }
1345 ImplTraitContext::Universal(in_band_ty_params) => {
0731742a 1346 // Add a definition for the in-band `Param`.
f035d41b 1347 let def_id = self.resolver.local_def_id(def_node_id);
ff7c6d11 1348
8faf50e0
XL
1349 let hir_bounds = self.lower_param_bounds(
1350 bounds,
1351 ImplTraitContext::Universal(in_band_ty_params),
ff7c6d11 1352 );
0731742a 1353 // Set the name to `impl Bound1 + Bound2`.
e1599b0c 1354 let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
8faf50e0 1355 in_band_ty_params.push(hir::GenericParam {
48663c56 1356 hir_id: self.lower_node_id(def_node_id),
8faf50e0
XL
1357 name: ParamName::Plain(ident),
1358 pure_wrt_drop: false,
dfeec247 1359 attrs: &[],
ff7c6d11 1360 bounds: hir_bounds,
ff7c6d11 1361 span,
8faf50e0
XL
1362 kind: hir::GenericParamKind::Type {
1363 default: None,
1364 synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
dfeec247 1365 },
ff7c6d11
XL
1366 });
1367
8faf50e0 1368 hir::TyKind::Path(hir::QPath::Resolved(
0531ce1d 1369 None,
dfeec247 1370 self.arena.alloc(hir::Path {
0531ce1d 1371 span,
ba9703b0 1372 res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
dfeec247 1373 segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
0531ce1d
XL
1374 }),
1375 ))
1376 }
0bf4aa26 1377 ImplTraitContext::Disallowed(pos) => {
dfeec247 1378 let allowed_in = if self.sess.features_untracked().impl_trait_in_bindings {
0bf4aa26
XL
1379 "bindings or function and inherent method return types"
1380 } else {
1381 "function and inherent method return types"
1382 };
1383 let mut err = struct_span_err!(
0531ce1d
XL
1384 self.sess,
1385 t.span,
1386 E0562,
0bf4aa26
XL
1387 "`impl Trait` not allowed outside of {}",
1388 allowed_in,
0531ce1d 1389 );
dfeec247
XL
1390 if pos == ImplTraitPosition::Binding && nightly_options::is_nightly_build()
1391 {
1392 err.help(
1393 "add `#![feature(impl_trait_in_bindings)]` to the crate \
1394 attributes to enable",
1395 );
0bf4aa26
XL
1396 }
1397 err.emit();
8faf50e0 1398 hir::TyKind::Err
abe05a73
XL
1399 }
1400 }
cc61c64b 1401 }
ba9703b0 1402 TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
dfeec247
XL
1403 TyKind::CVarArgs => {
1404 self.sess.delay_span_bug(
1405 t.span,
1406 "`TyKind::CVarArgs` should have been handled elsewhere",
1407 );
1408 hir::TyKind::Err
1409 }
cc61c64b
XL
1410 };
1411
dfeec247 1412 hir::Ty { kind, span: t.span, hir_id: self.lower_node_id(t.id) }
8faf50e0
XL
1413 }
1414
416331ca 1415 fn lower_opaque_impl_trait(
8faf50e0
XL
1416 &mut self,
1417 span: Span,
0bf4aa26 1418 fn_def_id: Option<DefId>,
74b04a01 1419 origin: hir::OpaqueTyOrigin,
416331ca 1420 opaque_ty_node_id: NodeId,
f035d41b 1421 capturable_lifetimes: Option<&FxHashSet<hir::LifetimeName>>,
dfeec247
XL
1422 lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
1423 ) -> hir::TyKind<'hir> {
e1599b0c
XL
1424 debug!(
1425 "lower_opaque_impl_trait(fn_def_id={:?}, opaque_ty_node_id={:?}, span={:?})",
dfeec247 1426 fn_def_id, opaque_ty_node_id, span,
e1599b0c
XL
1427 );
1428
8faf50e0
XL
1429 // Make sure we know that some funky desugaring has been going on here.
1430 // This is a first: there is code in other places like for loop
1431 // desugaring that explicitly states that we don't want to track that.
dc9dc135 1432 // Not tracking it makes lints in rustc and clippy very fragile, as
8faf50e0 1433 // frequently opened issues show.
dfeec247 1434 let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
8faf50e0 1435
f035d41b 1436 let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
8faf50e0 1437
416331ca 1438 self.allocate_hir_id_counter(opaque_ty_node_id);
8faf50e0 1439
416331ca 1440 let hir_bounds = self.with_hir_id_owner(opaque_ty_node_id, lower_bounds);
8faf50e0 1441
f035d41b
XL
1442 let (lifetimes, lifetime_defs) = self.lifetimes_from_impl_trait_bounds(
1443 opaque_ty_node_id,
1444 opaque_ty_def_id,
1445 &hir_bounds,
1446 capturable_lifetimes,
1447 );
8faf50e0 1448
f035d41b 1449 debug!("lower_opaque_impl_trait: lifetimes={:#?}", lifetimes);
e1599b0c 1450
f035d41b 1451 debug!("lower_opaque_impl_trait: lifetime_defs={:#?}", lifetime_defs);
e1599b0c 1452
dfeec247 1453 self.with_hir_id_owner(opaque_ty_node_id, move |lctx| {
416331ca 1454 let opaque_ty_item = hir::OpaqueTy {
8faf50e0
XL
1455 generics: hir::Generics {
1456 params: lifetime_defs,
dfeec247 1457 where_clause: hir::WhereClause { predicates: &[], span },
8faf50e0
XL
1458 span,
1459 },
1460 bounds: hir_bounds,
0bf4aa26 1461 impl_trait_fn: fn_def_id,
74b04a01 1462 origin,
8faf50e0
XL
1463 };
1464
ba9703b0 1465 trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id);
dfeec247
XL
1466 let opaque_ty_id =
1467 lctx.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
8faf50e0 1468
0731742a 1469 // `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
f035d41b 1470 hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, lifetimes)
a7813a04
XL
1471 })
1472 }
e9174d1e 1473
416331ca
XL
1474 /// Registers a new opaque type with the proper `NodeId`s and
1475 /// returns the lowered node-ID for the opaque type.
1476 fn generate_opaque_type(
532ac7d7 1477 &mut self,
416331ca 1478 opaque_ty_node_id: NodeId,
dfeec247 1479 opaque_ty_item: hir::OpaqueTy<'hir>,
532ac7d7 1480 span: Span,
416331ca 1481 opaque_ty_span: Span,
48663c56 1482 ) -> hir::HirId {
416331ca
XL
1483 let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
1484 let opaque_ty_id = self.lower_node_id(opaque_ty_node_id);
1485 // Generate an `type Foo = impl Trait;` declaration.
1486 trace!("registering opaque type with id {:#?}", opaque_ty_id);
1487 let opaque_ty_item = hir::Item {
1488 hir_id: opaque_ty_id,
dc9dc135 1489 ident: Ident::invalid(),
532ac7d7 1490 attrs: Default::default(),
e74abb32 1491 kind: opaque_ty_item_kind,
532ac7d7 1492 vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited),
416331ca 1493 span: opaque_ty_span,
532ac7d7
XL
1494 };
1495
1496 // Insert the item into the global item list. This usually happens
416331ca 1497 // automatically for all AST items. But this opaque type item
532ac7d7 1498 // does not actually exist in the AST.
416331ca
XL
1499 self.insert_item(opaque_ty_item);
1500 opaque_ty_id
532ac7d7
XL
1501 }
1502
ff7c6d11
XL
1503 fn lifetimes_from_impl_trait_bounds(
1504 &mut self,
416331ca 1505 opaque_ty_id: NodeId,
ba9703b0 1506 parent_def_id: LocalDefId,
dfeec247 1507 bounds: hir::GenericBounds<'hir>,
f035d41b 1508 lifetimes_to_include: Option<&FxHashSet<hir::LifetimeName>>,
dfeec247 1509 ) -> (&'hir [hir::GenericArg<'hir>], &'hir [hir::GenericParam<'hir>]) {
e1599b0c
XL
1510 debug!(
1511 "lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \
ba9703b0 1512 parent_def_id={:?}, \
e1599b0c 1513 bounds={:#?})",
ba9703b0 1514 opaque_ty_id, parent_def_id, bounds,
e1599b0c
XL
1515 );
1516
dc9dc135 1517 // This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
ff7c6d11 1518 // appear in the bounds, excluding lifetimes that are created within the bounds.
0731742a 1519 // E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`.
dfeec247
XL
1520 struct ImplTraitLifetimeCollector<'r, 'a, 'hir> {
1521 context: &'r mut LoweringContext<'a, 'hir>,
ba9703b0 1522 parent: LocalDefId,
416331ca 1523 opaque_ty_id: NodeId,
ff7c6d11
XL
1524 collect_elided_lifetimes: bool,
1525 currently_bound_lifetimes: Vec<hir::LifetimeName>,
b7449926 1526 already_defined_lifetimes: FxHashSet<hir::LifetimeName>,
dfeec247
XL
1527 output_lifetimes: Vec<hir::GenericArg<'hir>>,
1528 output_lifetime_params: Vec<hir::GenericParam<'hir>>,
f035d41b 1529 lifetimes_to_include: Option<&'r FxHashSet<hir::LifetimeName>>,
ff7c6d11
XL
1530 }
1531
dfeec247 1532 impl<'r, 'a, 'v, 'hir> intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a, 'hir> {
ba9703b0 1533 type Map = intravisit::ErasedMap<'v>;
dfeec247 1534
ba9703b0 1535 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
dfeec247 1536 intravisit::NestedVisitorMap::None
ff7c6d11
XL
1537 }
1538
dfeec247 1539 fn visit_generic_args(&mut self, span: Span, parameters: &'v hir::GenericArgs<'v>) {
ff7c6d11
XL
1540 // Don't collect elided lifetimes used inside of `Fn()` syntax.
1541 if parameters.parenthesized {
1542 let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1543 self.collect_elided_lifetimes = false;
dfeec247 1544 intravisit::walk_generic_args(self, span, parameters);
ff7c6d11
XL
1545 self.collect_elided_lifetimes = old_collect_elided_lifetimes;
1546 } else {
dfeec247 1547 intravisit::walk_generic_args(self, span, parameters);
ff7c6d11
XL
1548 }
1549 }
1550
dfeec247 1551 fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
0731742a 1552 // Don't collect elided lifetimes used inside of `fn()` syntax.
e74abb32 1553 if let hir::TyKind::BareFn(_) = t.kind {
ff7c6d11
XL
1554 let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
1555 self.collect_elided_lifetimes = false;
94b46f34
XL
1556
1557 // Record the "stack height" of `for<'a>` lifetime bindings
1558 // to be able to later fully undo their introduction.
1559 let old_len = self.currently_bound_lifetimes.len();
dfeec247 1560 intravisit::walk_ty(self, t);
94b46f34
XL
1561 self.currently_bound_lifetimes.truncate(old_len);
1562
ff7c6d11
XL
1563 self.collect_elided_lifetimes = old_collect_elided_lifetimes;
1564 } else {
dfeec247 1565 intravisit::walk_ty(self, t)
ff7c6d11
XL
1566 }
1567 }
1568
0531ce1d
XL
1569 fn visit_poly_trait_ref(
1570 &mut self,
dfeec247 1571 trait_ref: &'v hir::PolyTraitRef<'v>,
94b46f34 1572 modifier: hir::TraitBoundModifier,
0531ce1d 1573 ) {
94b46f34
XL
1574 // Record the "stack height" of `for<'a>` lifetime bindings
1575 // to be able to later fully undo their introduction.
ff7c6d11 1576 let old_len = self.currently_bound_lifetimes.len();
dfeec247 1577 intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
94b46f34
XL
1578 self.currently_bound_lifetimes.truncate(old_len);
1579 }
ff7c6d11 1580
dfeec247 1581 fn visit_generic_param(&mut self, param: &'v hir::GenericParam<'v>) {
0731742a 1582 // Record the introduction of 'a in `for<'a> ...`.
8faf50e0 1583 if let hir::GenericParamKind::Lifetime { .. } = param.kind {
94b46f34 1584 // Introduce lifetimes one at a time so that we can handle
0731742a 1585 // cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`.
8faf50e0
XL
1586 let lt_name = hir::LifetimeName::Param(param.name);
1587 self.currently_bound_lifetimes.push(lt_name);
ff7c6d11
XL
1588 }
1589
dfeec247 1590 intravisit::walk_generic_param(self, param);
ff7c6d11
XL
1591 }
1592
1593 fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
1594 let name = match lifetime.name {
0531ce1d 1595 hir::LifetimeName::Implicit | hir::LifetimeName::Underscore => {
ff7c6d11
XL
1596 if self.collect_elided_lifetimes {
1597 // Use `'_` for both implicit and underscore lifetimes in
416331ca 1598 // `type Foo<'_> = impl SomeTrait<'_>;`.
ff7c6d11
XL
1599 hir::LifetimeName::Underscore
1600 } else {
0531ce1d 1601 return;
ff7c6d11 1602 }
0531ce1d 1603 }
8faf50e0 1604 hir::LifetimeName::Param(_) => lifetime.name,
e1599b0c
XL
1605
1606 // Refers to some other lifetime that is "in
1607 // scope" within the type.
1608 hir::LifetimeName::ImplicitObjectLifetimeDefault => return,
1609
0bf4aa26 1610 hir::LifetimeName::Error | hir::LifetimeName::Static => return,
ff7c6d11
XL
1611 };
1612
0531ce1d 1613 if !self.currently_bound_lifetimes.contains(&name)
dfeec247 1614 && !self.already_defined_lifetimes.contains(&name)
f035d41b 1615 && self.lifetimes_to_include.map_or(true, |lifetimes| lifetimes.contains(&name))
dfeec247 1616 {
ff7c6d11
XL
1617 self.already_defined_lifetimes.insert(name);
1618
8faf50e0 1619 self.output_lifetimes.push(hir::GenericArg::Lifetime(hir::Lifetime {
48663c56 1620 hir_id: self.context.next_id(),
ff7c6d11
XL
1621 span: lifetime.span,
1622 name,
8faf50e0 1623 }));
ff7c6d11 1624
60c5eb7d 1625 let def_node_id = self.context.resolver.next_node_id();
48663c56 1626 let hir_id =
416331ca 1627 self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
f035d41b 1628 self.context.resolver.create_def(
ff7c6d11
XL
1629 self.parent,
1630 def_node_id,
e74abb32 1631 DefPathData::LifetimeNs(name.ident().name),
416331ca 1632 ExpnId::root(),
dfeec247
XL
1633 lifetime.span,
1634 );
8faf50e0 1635
0bf4aa26
XL
1636 let (name, kind) = match name {
1637 hir::LifetimeName::Underscore => (
e1599b0c 1638 hir::ParamName::Plain(Ident::with_dummy_span(kw::UnderscoreLifetime)),
0bf4aa26
XL
1639 hir::LifetimeParamKind::Elided,
1640 ),
dfeec247
XL
1641 hir::LifetimeName::Param(param_name) => {
1642 (param_name, hir::LifetimeParamKind::Explicit)
1643 }
ba9703b0 1644 _ => panic!("expected `LifetimeName::Param` or `ParamName::Plain`"),
8faf50e0
XL
1645 };
1646
1647 self.output_lifetime_params.push(hir::GenericParam {
9fa01778 1648 hir_id,
94b46f34 1649 name,
8faf50e0
XL
1650 span: lifetime.span,
1651 pure_wrt_drop: false,
dfeec247
XL
1652 attrs: &[],
1653 bounds: &[],
1654 kind: hir::GenericParamKind::Lifetime { kind },
8faf50e0 1655 });
ff7c6d11
XL
1656 }
1657 }
1658 }
1659
1660 let mut lifetime_collector = ImplTraitLifetimeCollector {
1661 context: self,
ba9703b0 1662 parent: parent_def_id,
416331ca 1663 opaque_ty_id,
ff7c6d11
XL
1664 collect_elided_lifetimes: true,
1665 currently_bound_lifetimes: Vec::new(),
b7449926 1666 already_defined_lifetimes: FxHashSet::default(),
ff7c6d11
XL
1667 output_lifetimes: Vec::new(),
1668 output_lifetime_params: Vec::new(),
f035d41b 1669 lifetimes_to_include,
ff7c6d11
XL
1670 };
1671
1672 for bound in bounds {
dfeec247 1673 intravisit::walk_param_bound(&mut lifetime_collector, &bound);
ff7c6d11
XL
1674 }
1675
dfeec247
XL
1676 let ImplTraitLifetimeCollector { output_lifetimes, output_lifetime_params, .. } =
1677 lifetime_collector;
32a655c1 1678
dc9dc135 1679 (
dfeec247
XL
1680 self.arena.alloc_from_iter(output_lifetimes),
1681 self.arena.alloc_from_iter(output_lifetime_params),
0531ce1d 1682 )
e9174d1e 1683 }
e9174d1e 1684
dfeec247 1685 fn lower_local(&mut self, l: &Local) -> (hir::Local<'hir>, SmallVec<[NodeId; 1]>) {
532ac7d7 1686 let mut ids = SmallVec::<[NodeId; 1]>::new();
0bf4aa26
XL
1687 if self.sess.features_untracked().impl_trait_in_bindings {
1688 if let Some(ref ty) = l.ty {
1689 let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
1690 visitor.visit_ty(ty);
1691 }
1692 }
dfeec247 1693 let ty = l.ty.as_ref().map(|t| {
f035d41b 1694 let mut capturable_lifetimes;
dfeec247
XL
1695 self.lower_ty(
1696 t,
1697 if self.sess.features_untracked().impl_trait_in_bindings {
f035d41b
XL
1698 capturable_lifetimes = FxHashSet::default();
1699 ImplTraitContext::OtherOpaqueTy {
1700 capturable_lifetimes: &mut capturable_lifetimes,
1701 origin: hir::OpaqueTyOrigin::Binding,
1702 }
dfeec247
XL
1703 } else {
1704 ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
1705 },
1706 )
1707 });
1708 let init = l.init.as_ref().map(|e| self.lower_expr(e));
1709 (
1710 hir::Local {
1711 hir_id: self.lower_node_id(l.id),
1712 ty,
1713 pat: self.lower_pat(&l.pat),
1714 init,
1715 span: l.span,
1716 attrs: l.attrs.clone(),
1717 source: hir::LocalSource::Normal,
1718 },
1719 ids,
1720 )
a7813a04 1721 }
e9174d1e 1722
dfeec247 1723 fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
e74abb32
XL
1724 // Skip the `...` (`CVarArgs`) trailing arguments from the AST,
1725 // as they are not explicit in HIR/Ty function signatures.
1726 // (instead, the `c_variadic` flag is set to `true`)
1727 let mut inputs = &decl.inputs[..];
1728 if decl.c_variadic() {
1729 inputs = &inputs[..inputs.len() - 1];
1730 }
dfeec247
XL
1731 self.arena.alloc_from_iter(inputs.iter().map(|param| match param.pat.kind {
1732 PatKind::Ident(_, ident, _) => ident,
1733 _ => Ident::new(kw::Invalid, param.pat.span),
1734 }))
32a655c1
SL
1735 }
1736
8faf50e0
XL
1737 // Lowers a function declaration.
1738 //
dc9dc135
XL
1739 // `decl`: the unlowered (AST) function declaration.
1740 // `fn_def_id`: if `Some`, impl Trait arguments are lowered into generic parameters on the
8faf50e0 1741 // given DefId, otherwise impl Trait is disallowed. Must be `Some` if
dc9dc135
XL
1742 // `make_ret_async` is also `Some`.
1743 // `impl_trait_return_allow`: determines whether `impl Trait` can be used in return position.
1744 // This guards against trait declarations and implementations where `impl Trait` is
8faf50e0 1745 // disallowed.
dc9dc135
XL
1746 // `make_ret_async`: if `Some`, converts `-> T` into `-> impl Future<Output = T>` in the
1747 // return type. This is used for `async fn` declarations. The `NodeId` is the ID of the
1748 // return type `impl Trait` item.
0531ce1d
XL
1749 fn lower_fn_decl(
1750 &mut self,
1751 decl: &FnDecl,
dfeec247 1752 mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam<'hir>>)>,
0531ce1d 1753 impl_trait_return_allow: bool,
8faf50e0 1754 make_ret_async: Option<NodeId>,
dfeec247
XL
1755 ) -> &'hir hir::FnDecl<'hir> {
1756 debug!(
1757 "lower_fn_decl(\
60c5eb7d
XL
1758 fn_decl: {:?}, \
1759 in_band_ty_params: {:?}, \
1760 impl_trait_return_allow: {}, \
1761 make_ret_async: {:?})",
dfeec247 1762 decl, in_band_ty_params, impl_trait_return_allow, make_ret_async,
60c5eb7d 1763 );
532ac7d7
XL
1764 let lt_mode = if make_ret_async.is_some() {
1765 // In `async fn`, argument-position elided lifetimes
1766 // must be transformed into fresh generic parameters so that
416331ca 1767 // they can be applied to the opaque `impl Trait` return type.
532ac7d7
XL
1768 AnonymousLifetimeMode::CreateParameter
1769 } else {
1770 self.anonymous_lifetime_mode
1771 };
1772
e74abb32
XL
1773 let c_variadic = decl.c_variadic();
1774
532ac7d7
XL
1775 // Remember how many lifetimes were already around so that we can
1776 // only look at the lifetime parameters introduced by the arguments.
532ac7d7 1777 let inputs = self.with_anonymous_lifetime_mode(lt_mode, |this| {
e74abb32
XL
1778 // Skip the `...` (`CVarArgs`) trailing arguments from the AST,
1779 // as they are not explicit in HIR/Ty function signatures.
1780 // (instead, the `c_variadic` flag is set to `true`)
1781 let mut inputs = &decl.inputs[..];
1782 if c_variadic {
1783 inputs = &inputs[..inputs.len() - 1];
1784 }
dfeec247
XL
1785 this.arena.alloc_from_iter(inputs.iter().map(|param| {
1786 if let Some((_, ibty)) = &mut in_band_ty_params {
1787 this.lower_ty_direct(&param.ty, ImplTraitContext::Universal(ibty))
1788 } else {
1789 this.lower_ty_direct(&param.ty, ImplTraitContext::disallowed())
1790 }
1791 }))
532ac7d7 1792 });
8faf50e0
XL
1793
1794 let output = if let Some(ret_id) = make_ret_async {
1795 self.lower_async_fn_ret_ty(
8faf50e0 1796 &decl.output,
dc9dc135 1797 in_band_ty_params.expect("`make_ret_async` but no `fn_def_id`").0,
8faf50e0
XL
1798 ret_id,
1799 )
1800 } else {
1801 match decl.output {
74b04a01
XL
1802 FnRetTy::Ty(ref ty) => {
1803 let context = match in_band_ty_params {
1804 Some((def_id, _)) if impl_trait_return_allow => {
f035d41b
XL
1805 ImplTraitContext::ReturnPositionOpaqueTy {
1806 fn_def_id: def_id,
1807 origin: hir::OpaqueTyOrigin::FnReturn,
1808 }
74b04a01
XL
1809 }
1810 _ => ImplTraitContext::disallowed(),
1811 };
1812 hir::FnRetTy::Return(self.lower_ty(ty, context))
1813 }
1814 FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(span),
8faf50e0
XL
1815 }
1816 };
1817
dfeec247 1818 self.arena.alloc(hir::FnDecl {
8faf50e0
XL
1819 inputs,
1820 output,
e74abb32 1821 c_variadic,
dfeec247
XL
1822 implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
1823 let is_mutable_pat = match arg.pat.kind {
ba9703b0
XL
1824 PatKind::Ident(BindingMode::ByValue(mt) | BindingMode::ByRef(mt), _, _) => {
1825 mt == Mutability::Mut
1826 }
dfeec247
XL
1827 _ => false,
1828 };
0bf4aa26 1829
dfeec247
XL
1830 match arg.ty.kind {
1831 TyKind::ImplicitSelf if is_mutable_pat => hir::ImplicitSelfKind::Mut,
1832 TyKind::ImplicitSelf => hir::ImplicitSelfKind::Imm,
1833 // Given we are only considering `ImplicitSelf` types, we needn't consider
1834 // the case where we have a mutable pattern to a reference as that would
1835 // no longer be an `ImplicitSelf`.
1836 TyKind::Rptr(_, ref mt)
1837 if mt.ty.kind.is_implicit_self() && mt.mutbl == ast::Mutability::Mut =>
1838 {
1839 hir::ImplicitSelfKind::MutRef
0bf4aa26 1840 }
dfeec247
XL
1841 TyKind::Rptr(_, ref mt) if mt.ty.kind.is_implicit_self() => {
1842 hir::ImplicitSelfKind::ImmRef
1843 }
1844 _ => hir::ImplicitSelfKind::None,
1845 }
1846 }),
a7813a04 1847 })
b039eaaf 1848 }
e9174d1e 1849
416331ca
XL
1850 // Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
1851 // combined with the following definition of `OpaqueTy`:
532ac7d7 1852 //
416331ca 1853 // type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
8faf50e0 1854 //
e1599b0c 1855 // `inputs`: lowered types of parameters to the function (used to collect lifetimes)
dc9dc135
XL
1856 // `output`: unlowered output type (`T` in `-> T`)
1857 // `fn_def_id`: `DefId` of the parent function (used to create child impl trait definition)
416331ca 1858 // `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
dc9dc135 1859 // `elided_lt_replacement`: replacement for elided lifetimes in the return type
8faf50e0 1860 fn lower_async_fn_ret_ty(
0531ce1d 1861 &mut self,
74b04a01 1862 output: &FnRetTy,
8faf50e0 1863 fn_def_id: DefId,
416331ca 1864 opaque_ty_node_id: NodeId,
74b04a01 1865 ) -> hir::FnRetTy<'hir> {
e1599b0c
XL
1866 debug!(
1867 "lower_async_fn_ret_ty(\
1868 output={:?}, \
1869 fn_def_id={:?}, \
1870 opaque_ty_node_id={:?})",
1871 output, fn_def_id, opaque_ty_node_id,
1872 );
1873
532ac7d7 1874 let span = output.span();
8faf50e0 1875
dfeec247 1876 let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
8faf50e0 1877
f035d41b 1878 let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
8faf50e0 1879
416331ca 1880 self.allocate_hir_id_counter(opaque_ty_node_id);
8faf50e0 1881
e1599b0c
XL
1882 // When we create the opaque type for this async fn, it is going to have
1883 // to capture all the lifetimes involved in the signature (including in the
1884 // return type). This is done by introducing lifetime parameters for:
1885 //
1886 // - all the explicitly declared lifetimes from the impl and function itself;
1887 // - all the elided lifetimes in the fn arguments;
1888 // - all the elided lifetimes in the return type.
1889 //
1890 // So for example in this snippet:
1891 //
1892 // ```rust
1893 // impl<'a> Foo<'a> {
1894 // async fn bar<'b>(&self, x: &'b Vec<f64>, y: &str) -> &u32 {
1895 // // ^ '0 ^ '1 ^ '2
1896 // // elided lifetimes used below
1897 // }
1898 // }
1899 // ```
1900 //
1901 // we would create an opaque type like:
1902 //
1903 // ```
1904 // type Bar<'a, 'b, '0, '1, '2> = impl Future<Output = &'2 u32>;
1905 // ```
1906 //
1907 // and we would then desugar `bar` to the equivalent of:
1908 //
1909 // ```rust
1910 // impl<'a> Foo<'a> {
1911 // fn bar<'b, '0, '1>(&'0 self, x: &'b Vec<f64>, y: &'1 str) -> Bar<'a, 'b, '0, '1, '_>
1912 // }
1913 // ```
1914 //
1915 // Note that the final parameter to `Bar` is `'_`, not `'2` --
1916 // this is because the elided lifetimes from the return type
1917 // should be figured out using the ordinary elision rules, and
1918 // this desugaring achieves that.
1919 //
1920 // The variable `input_lifetimes_count` tracks the number of
1921 // lifetime parameters to the opaque type *not counting* those
1922 // lifetimes elided in the return type. This includes those
1923 // that are explicitly declared (`in_scope_lifetimes`) and
1924 // those elided lifetimes we found in the arguments (current
1925 // content of `lifetimes_to_define`). Next, we will process
1926 // the return type, which will cause `lifetimes_to_define` to
1927 // grow.
1928 let input_lifetimes_count = self.in_scope_lifetimes.len() + self.lifetimes_to_define.len();
1929
416331ca 1930 let (opaque_ty_id, lifetime_params) = self.with_hir_id_owner(opaque_ty_node_id, |this| {
e1599b0c
XL
1931 // We have to be careful to get elision right here. The
1932 // idea is that we create a lifetime parameter for each
1933 // lifetime in the return type. So, given a return type
1934 // like `async fn foo(..) -> &[&u32]`, we lower to `impl
1935 // Future<Output = &'1 [ &'2 u32 ]>`.
1936 //
1937 // Then, we will create `fn foo(..) -> Foo<'_, '_>`, and
1938 // hence the elision takes place at the fn site.
dfeec247
XL
1939 let future_bound = this
1940 .with_anonymous_lifetime_mode(AnonymousLifetimeMode::CreateParameter, |this| {
1941 this.lower_async_fn_output_type_to_future_bound(output, fn_def_id, span)
1942 });
8faf50e0 1943
e1599b0c
XL
1944 debug!("lower_async_fn_ret_ty: future_bound={:#?}", future_bound);
1945
532ac7d7 1946 // Calculate all the lifetimes that should be captured
416331ca 1947 // by the opaque type. This should include all in-scope
532ac7d7
XL
1948 // lifetime parameters, including those defined in-band.
1949 //
1950 // Note: this must be done after lowering the output type,
1951 // as the output type may introduce new in-band lifetimes.
dfeec247
XL
1952 let lifetime_params: Vec<(Span, ParamName)> = this
1953 .in_scope_lifetimes
1954 .iter()
1955 .cloned()
1956 .map(|name| (name.ident().span, name))
1957 .chain(this.lifetimes_to_define.iter().cloned())
1958 .collect();
8faf50e0 1959
e1599b0c
XL
1960 debug!("lower_async_fn_ret_ty: in_scope_lifetimes={:#?}", this.in_scope_lifetimes);
1961 debug!("lower_async_fn_ret_ty: lifetimes_to_define={:#?}", this.lifetimes_to_define);
1962 debug!("lower_async_fn_ret_ty: lifetime_params={:#?}", lifetime_params);
1963
532ac7d7 1964 let generic_params =
dfeec247 1965 this.arena.alloc_from_iter(lifetime_params.iter().map(|(span, hir_name)| {
ba9703b0 1966 this.lifetime_to_generic_param(*span, *hir_name, opaque_ty_def_id)
dfeec247 1967 }));
8faf50e0 1968
416331ca 1969 let opaque_ty_item = hir::OpaqueTy {
532ac7d7
XL
1970 generics: hir::Generics {
1971 params: generic_params,
dfeec247 1972 where_clause: hir::WhereClause { predicates: &[], span },
532ac7d7
XL
1973 span,
1974 },
dfeec247 1975 bounds: arena_vec![this; future_bound],
532ac7d7 1976 impl_trait_fn: Some(fn_def_id),
416331ca 1977 origin: hir::OpaqueTyOrigin::AsyncFn,
8faf50e0
XL
1978 };
1979
ba9703b0 1980 trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
dfeec247
XL
1981 let opaque_ty_id =
1982 this.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
8faf50e0 1983
416331ca 1984 (opaque_ty_id, lifetime_params)
532ac7d7 1985 });
8faf50e0 1986
e1599b0c
XL
1987 // As documented above on the variable
1988 // `input_lifetimes_count`, we need to create the lifetime
1989 // arguments to our opaque type. Continuing with our example,
1990 // we're creating the type arguments for the return type:
1991 //
1992 // ```
1993 // Bar<'a, 'b, '0, '1, '_>
1994 // ```
1995 //
1996 // For the "input" lifetime parameters, we wish to create
1997 // references to the parameters themselves, including the
1998 // "implicit" ones created from parameter types (`'a`, `'b`,
1999 // '`0`, `'1`).
2000 //
2001 // For the "output" lifetime parameters, we just want to
2002 // generate `'_`.
dfeec247 2003 let mut generic_args: Vec<_> = lifetime_params[..input_lifetimes_count]
e1599b0c
XL
2004 .iter()
2005 .map(|&(span, hir_name)| {
2006 // Input lifetime like `'a` or `'1`:
2007 GenericArg::Lifetime(hir::Lifetime {
2008 hir_id: self.next_id(),
2009 span,
2010 name: hir::LifetimeName::Param(hir_name),
532ac7d7 2011 })
e1599b0c
XL
2012 })
2013 .collect();
dfeec247
XL
2014 generic_args.extend(lifetime_params[input_lifetimes_count..].iter().map(|&(span, _)|
2015 // Output lifetime like `'_`.
2016 GenericArg::Lifetime(hir::Lifetime {
2017 hir_id: self.next_id(),
2018 span,
2019 name: hir::LifetimeName::Implicit,
2020 })));
2021 let generic_args = self.arena.alloc_from_iter(generic_args);
8faf50e0 2022
dfeec247 2023 // Create the `Foo<...>` reference itself. Note that the `type
e1599b0c
XL
2024 // Foo = impl Trait` is, internally, created as a child of the
2025 // async fn, so the *type parameters* are inherited. It's
2026 // only the lifetime parameters that we must supply.
f035d41b 2027 let opaque_ty_ref = hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, generic_args);
dfeec247 2028 let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
74b04a01 2029 hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
532ac7d7 2030 }
8faf50e0 2031
dc9dc135 2032 /// Transforms `-> T` into `Future<Output = T>`
532ac7d7
XL
2033 fn lower_async_fn_output_type_to_future_bound(
2034 &mut self,
74b04a01 2035 output: &FnRetTy,
532ac7d7
XL
2036 fn_def_id: DefId,
2037 span: Span,
dfeec247 2038 ) -> hir::GenericBound<'hir> {
532ac7d7
XL
2039 // Compute the `T` in `Future<Output = T>` from the return type.
2040 let output_ty = match output {
74b04a01
XL
2041 FnRetTy::Ty(ty) => {
2042 // Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
2043 // `impl Future` opaque type that `async fn` implicitly
2044 // generates.
f035d41b
XL
2045 let context = ImplTraitContext::ReturnPositionOpaqueTy {
2046 fn_def_id,
2047 origin: hir::OpaqueTyOrigin::FnReturn,
2048 };
74b04a01
XL
2049 self.lower_ty(ty, context)
2050 }
2051 FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
532ac7d7 2052 };
8faf50e0 2053
532ac7d7 2054 // "<Output = T>"
3dfed10e 2055 let future_args = self.arena.alloc(hir::GenericArgs {
dfeec247
XL
2056 args: &[],
2057 bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
532ac7d7 2058 parenthesized: false,
8faf50e0
XL
2059 });
2060
3dfed10e
XL
2061 hir::GenericBound::LangItemTrait(
2062 // ::std::future::Future<future_params>
2063 hir::LangItem::Future,
2064 span,
2065 self.next_id(),
2066 future_args,
532ac7d7 2067 )
b039eaaf 2068 }
e9174d1e 2069
8faf50e0 2070 fn lower_param_bound(
0531ce1d 2071 &mut self,
8faf50e0 2072 tpb: &GenericBound,
dfeec247
XL
2073 itctx: ImplTraitContext<'_, 'hir>,
2074 ) -> hir::GenericBound<'hir> {
8faf50e0 2075 match *tpb {
dfeec247
XL
2076 GenericBound::Trait(ref ty, modifier) => hir::GenericBound::Trait(
2077 self.lower_poly_trait_ref(ty, itctx),
2078 self.lower_trait_bound_modifier(modifier),
2079 ),
8faf50e0
XL
2080 GenericBound::Outlives(ref lifetime) => {
2081 hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
2082 }
e9174d1e 2083 }
e9174d1e 2084 }
e9174d1e 2085
a7813a04 2086 fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
83c7162d 2087 let span = l.ident.span;
8faf50e0 2088 match l.ident {
dfeec247
XL
2089 ident if ident.name == kw::StaticLifetime => {
2090 self.new_named_lifetime(l.id, span, hir::LifetimeName::Static)
2091 }
2092 ident if ident.name == kw::UnderscoreLifetime => match self.anonymous_lifetime_mode {
2093 AnonymousLifetimeMode::CreateParameter => {
2094 let fresh_name = self.collect_fresh_in_band_lifetime(span);
2095 self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
2096 }
ff7c6d11 2097
dfeec247
XL
2098 AnonymousLifetimeMode::PassThrough => {
2099 self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
2100 }
0bf4aa26 2101
dfeec247
XL
2102 AnonymousLifetimeMode::ReportError => self.new_error_lifetime(Some(l.id), span),
2103 },
8faf50e0
XL
2104 ident => {
2105 self.maybe_collect_in_band_lifetime(ident);
2106 let param_name = ParamName::Plain(ident);
2107 self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
ff7c6d11 2108 }
0531ce1d
XL
2109 }
2110 }
ff7c6d11 2111
0531ce1d
XL
2112 fn new_named_lifetime(
2113 &mut self,
2114 id: NodeId,
2115 span: Span,
2116 name: hir::LifetimeName,
2117 ) -> hir::Lifetime {
dfeec247
XL
2118 hir::Lifetime { hir_id: self.lower_node_id(id), span, name }
2119 }
2120
2121 fn lower_generic_params_mut<'s>(
2122 &'s mut self,
2123 params: &'s [GenericParam],
2124 add_bounds: &'s NodeMap<Vec<GenericBound>>,
2125 mut itctx: ImplTraitContext<'s, 'hir>,
2126 ) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {
2127 params
2128 .iter()
2129 .map(move |param| self.lower_generic_param(param, add_bounds, itctx.reborrow()))
b039eaaf 2130 }
e9174d1e 2131
8faf50e0
XL
2132 fn lower_generic_params(
2133 &mut self,
2134 params: &[GenericParam],
2135 add_bounds: &NodeMap<Vec<GenericBound>>,
dfeec247
XL
2136 itctx: ImplTraitContext<'_, 'hir>,
2137 ) -> &'hir [hir::GenericParam<'hir>] {
2138 self.arena.alloc_from_iter(self.lower_generic_params_mut(params, add_bounds, itctx))
2139 }
2140
2141 fn lower_generic_param(
2142 &mut self,
2143 param: &GenericParam,
2144 add_bounds: &NodeMap<Vec<GenericBound>>,
2145 mut itctx: ImplTraitContext<'_, 'hir>,
2146 ) -> hir::GenericParam<'hir> {
2147 let mut bounds: Vec<_> = self
2148 .with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
2149 this.lower_param_bounds_mut(&param.bounds, itctx.reborrow()).collect()
2150 });
0bf4aa26 2151
9fa01778 2152 let (name, kind) = match param.kind {
8faf50e0
XL
2153 GenericParamKind::Lifetime => {
2154 let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
2155 self.is_collecting_in_band_lifetimes = false;
2156
dfeec247
XL
2157 let lt = self
2158 .with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
2159 this.lower_lifetime(&Lifetime { id: param.id, ident: param.ident })
2160 });
8faf50e0
XL
2161 let param_name = match lt.name {
2162 hir::LifetimeName::Param(param_name) => param_name,
0bf4aa26 2163 hir::LifetimeName::Implicit
dfeec247
XL
2164 | hir::LifetimeName::Underscore
2165 | hir::LifetimeName::Static => hir::ParamName::Plain(lt.name.ident()),
e1599b0c 2166 hir::LifetimeName::ImplicitObjectLifetimeDefault => {
ba9703b0 2167 self.sess.diagnostic().span_bug(
e1599b0c
XL
2168 param.ident.span,
2169 "object-lifetime-default should not occur here",
2170 );
2171 }
0bf4aa26 2172 hir::LifetimeName::Error => ParamName::Error,
8faf50e0 2173 };
9fa01778 2174
dfeec247
XL
2175 let kind =
2176 hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
ff7c6d11 2177
8faf50e0 2178 self.is_collecting_in_band_lifetimes = was_collecting_in_band;
ff7c6d11 2179
9fa01778 2180 (param_name, kind)
8faf50e0
XL
2181 }
2182 GenericParamKind::Type { ref default, .. } => {
8faf50e0
XL
2183 let add_bounds = add_bounds.get(&param.id).map_or(&[][..], |x| &x);
2184 if !add_bounds.is_empty() {
dfeec247
XL
2185 let params = self.lower_param_bounds_mut(add_bounds, itctx.reborrow());
2186 bounds.extend(params);
8faf50e0 2187 }
e9174d1e 2188
9fa01778 2189 let kind = hir::GenericParamKind::Type {
74b04a01
XL
2190 default: default.as_ref().map(|x| {
2191 self.lower_ty(
2192 x,
f035d41b
XL
2193 ImplTraitContext::OtherOpaqueTy {
2194 capturable_lifetimes: &mut FxHashSet::default(),
2195 origin: hir::OpaqueTyOrigin::Misc,
2196 },
74b04a01
XL
2197 )
2198 }),
dfeec247
XL
2199 synthetic: param
2200 .attrs
2201 .iter()
3dfed10e 2202 .filter(|attr| self.sess.check_name(attr, sym::rustc_synthetic))
dfeec247
XL
2203 .map(|_| hir::SyntheticTyParamKind::ImplTrait)
2204 .next(),
9fa01778
XL
2205 };
2206
e1599b0c 2207 (hir::ParamName::Plain(param.ident), kind)
8faf50e0 2208 }
f035d41b 2209 GenericParamKind::Const { ref ty, kw_span: _ } => {
dfeec247
XL
2210 let ty = self
2211 .with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
2212 this.lower_ty(&ty, ImplTraitContext::disallowed())
2213 });
2214
2215 (hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty })
9fa01778
XL
2216 }
2217 };
2218
9fa01778 2219 hir::GenericParam {
48663c56 2220 hir_id: self.lower_node_id(param.id),
9fa01778
XL
2221 name,
2222 span: param.ident.span,
3dfed10e 2223 pure_wrt_drop: self.sess.contains_name(&param.attrs, sym::may_dangle),
9fa01778 2224 attrs: self.lower_attrs(&param.attrs),
dfeec247 2225 bounds: self.arena.alloc_from_iter(bounds),
9fa01778 2226 kind,
8faf50e0 2227 }
e9174d1e 2228 }
e9174d1e 2229
dfeec247
XL
2230 fn lower_trait_ref(
2231 &mut self,
2232 p: &TraitRef,
2233 itctx: ImplTraitContext<'_, 'hir>,
2234 ) -> hir::TraitRef<'hir> {
abe05a73 2235 let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
416331ca 2236 hir::QPath::Resolved(None, path) => path,
ba9703b0 2237 qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
32a655c1 2238 };
dfeec247 2239 hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id) }
e9174d1e 2240 }
e9174d1e 2241
0531ce1d
XL
2242 fn lower_poly_trait_ref(
2243 &mut self,
2244 p: &PolyTraitRef,
dfeec247
XL
2245 mut itctx: ImplTraitContext<'_, 'hir>,
2246 ) -> hir::PolyTraitRef<'hir> {
a1dfa0c6
XL
2247 let bound_generic_params = self.lower_generic_params(
2248 &p.bound_generic_params,
2249 &NodeMap::default(),
2250 itctx.reborrow(),
2251 );
f035d41b 2252
dfeec247 2253 let trait_ref = self.with_in_scope_lifetime_defs(&p.bound_generic_params, |this| {
f035d41b
XL
2254 // Any impl Trait types defined within this scope can capture
2255 // lifetimes bound on this predicate.
2256 let lt_def_names = p.bound_generic_params.iter().filter_map(|param| match param.kind {
2257 GenericParamKind::Lifetime { .. } => Some(hir::LifetimeName::Param(
2258 ParamName::Plain(param.ident.normalize_to_macros_2_0()),
2259 )),
2260 _ => None,
2261 });
2262 if let ImplTraitContext::OtherOpaqueTy { ref mut capturable_lifetimes, .. } = itctx {
2263 capturable_lifetimes.extend(lt_def_names.clone());
2264 }
2265
2266 let res = this.lower_trait_ref(&p.trait_ref, itctx.reborrow());
2267
2268 if let ImplTraitContext::OtherOpaqueTy { ref mut capturable_lifetimes, .. } = itctx {
2269 for param in lt_def_names {
2270 capturable_lifetimes.remove(&param);
2271 }
2272 }
2273 res
dfeec247 2274 });
ff7c6d11 2275
dfeec247 2276 hir::PolyTraitRef { bound_generic_params, trait_ref, span: p.span }
b039eaaf 2277 }
e9174d1e 2278
dfeec247
XL
2279 fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext<'_, 'hir>) -> hir::MutTy<'hir> {
2280 hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
2281 }
2282
2283 fn lower_param_bounds(
2284 &mut self,
2285 bounds: &[GenericBound],
2286 itctx: ImplTraitContext<'_, 'hir>,
2287 ) -> hir::GenericBounds<'hir> {
2288 self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
a7813a04 2289 }
e9174d1e 2290
dfeec247
XL
2291 fn lower_param_bounds_mut<'s>(
2292 &'s mut self,
2293 bounds: &'s [GenericBound],
2294 mut itctx: ImplTraitContext<'s, 'hir>,
2295 ) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> {
2296 bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx.reborrow()))
a7813a04 2297 }
e9174d1e 2298
dfeec247
XL
2299 fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> &'hir hir::Block<'hir> {
2300 self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break))
2301 }
2302
2303 fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> {
e1599b0c 2304 let mut stmts = vec![];
dfeec247 2305 let mut expr: Option<&'hir _> = None;
3157f602 2306
cc61c64b
XL
2307 for (index, stmt) in b.stmts.iter().enumerate() {
2308 if index == b.stmts.len() - 1 {
e74abb32 2309 if let StmtKind::Expr(ref e) = stmt.kind {
dfeec247 2310 expr = Some(self.lower_expr(e));
cc61c64b
XL
2311 } else {
2312 stmts.extend(self.lower_stmt(stmt));
2313 }
3157f602 2314 } else {
cc61c64b 2315 stmts.extend(self.lower_stmt(stmt));
3157f602
XL
2316 }
2317 }
2318
dfeec247 2319 hir::Block {
48663c56 2320 hir_id: self.lower_node_id(b.id),
dfeec247 2321 stmts: self.arena.alloc_from_iter(stmts),
041b39d2 2322 expr,
a7813a04
XL
2323 rules: self.lower_block_check_mode(&b.rules),
2324 span: b.span,
041b39d2 2325 targeted_by_break,
dfeec247 2326 }
e9174d1e 2327 }
e9174d1e 2328
e1599b0c
XL
2329 /// Lowers a block directly to an expression, presuming that it
2330 /// has no attributes and is not targeted by a `break`.
dfeec247 2331 fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
e1599b0c 2332 let block = self.lower_block(b, false);
dfeec247 2333 self.expr_block(block, AttrVec::new())
a7813a04 2334 }
b039eaaf 2335
416331ca 2336 fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
dfeec247
XL
2337 self.with_new_scopes(|this| hir::AnonConst {
2338 hir_id: this.lower_node_id(c.id),
2339 body: this.lower_const_body(c.value.span, Some(&c.value)),
416331ca
XL
2340 })
2341 }
2342
dfeec247 2343 fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt<'hir>; 1]> {
e74abb32 2344 let kind = match s.kind {
0bf4aa26
XL
2345 StmtKind::Local(ref l) => {
2346 let (l, item_ids) = self.lower_local(l);
dfeec247 2347 let mut ids: SmallVec<[hir::Stmt<'hir>; 1]> = item_ids
0bf4aa26 2348 .into_iter()
9fa01778 2349 .map(|item_id| {
48663c56
XL
2350 let item_id = hir::ItemId { id: self.lower_node_id(item_id) };
2351 self.stmt(s.span, hir::StmtKind::Item(item_id))
0bf4aa26
XL
2352 })
2353 .collect();
9fa01778 2354 ids.push({
9fa01778 2355 hir::Stmt {
48663c56 2356 hir_id: self.lower_node_id(s.id),
dfeec247 2357 kind: hir::StmtKind::Local(self.arena.alloc(l)),
9fa01778
XL
2358 span: s.span,
2359 }
0bf4aa26
XL
2360 });
2361 return ids;
dfeec247 2362 }
476ff2be
SL
2363 StmtKind::Item(ref it) => {
2364 // Can only use the ID once.
2365 let mut id = Some(s.id);
dfeec247
XL
2366 return self
2367 .lower_item_id(it)
0531ce1d 2368 .into_iter()
9fa01778 2369 .map(|item_id| {
dfeec247
XL
2370 let hir_id = id
2371 .take()
2372 .map(|id| self.lower_node_id(id))
2373 .unwrap_or_else(|| self.next_id());
2374
2375 hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id), span: s.span }
0531ce1d
XL
2376 })
2377 .collect();
92a42be0 2378 }
dfeec247
XL
2379 StmtKind::Expr(ref e) => hir::StmtKind::Expr(self.lower_expr(e)),
2380 StmtKind::Semi(ref e) => hir::StmtKind::Semi(self.lower_expr(e)),
74b04a01 2381 StmtKind::Empty => return smallvec![],
ba9703b0 2382 StmtKind::MacCall(..) => panic!("shouldn't exist here"),
8faf50e0 2383 };
dfeec247 2384 smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id), kind, span: s.span }]
e9174d1e 2385 }
e9174d1e 2386
a7813a04
XL
2387 fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
2388 match *b {
dfeec247
XL
2389 BlockCheckMode::Default => hir::BlockCheckMode::DefaultBlock,
2390 BlockCheckMode::Unsafe(u) => {
2391 hir::BlockCheckMode::UnsafeBlock(self.lower_unsafe_source(u))
2392 }
a7813a04 2393 }
e9174d1e 2394 }
e9174d1e 2395
a7813a04
XL
2396 fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
2397 match u {
dfeec247
XL
2398 CompilerGenerated => hir::UnsafeSource::CompilerGenerated,
2399 UserProvided => hir::UnsafeSource::UserProvided,
a7813a04 2400 }
e9174d1e 2401 }
e9174d1e 2402
a7813a04
XL
2403 fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBoundModifier {
2404 match f {
2405 TraitBoundModifier::None => hir::TraitBoundModifier::None,
dfeec247
XL
2406 TraitBoundModifier::MaybeConst => hir::TraitBoundModifier::MaybeConst,
2407
2408 // `MaybeConstMaybe` will cause an error during AST validation, but we need to pick a
2409 // placeholder for compilation to proceed.
2410 TraitBoundModifier::MaybeConstMaybe | TraitBoundModifier::Maybe => {
2411 hir::TraitBoundModifier::Maybe
2412 }
a7813a04 2413 }
e9174d1e 2414 }
e9174d1e 2415
a7813a04 2416 // Helper methods for building HIR.
b039eaaf 2417
dfeec247 2418 fn stmt(&mut self, span: Span, kind: hir::StmtKind<'hir>) -> hir::Stmt<'hir> {
e74abb32 2419 hir::Stmt { span, kind, hir_id: self.next_id() }
48663c56
XL
2420 }
2421
dfeec247
XL
2422 fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {
2423 self.stmt(span, hir::StmtKind::Expr(self.arena.alloc(expr)))
416331ca
XL
2424 }
2425
0531ce1d
XL
2426 fn stmt_let_pat(
2427 &mut self,
dfeec247 2428 attrs: AttrVec,
48663c56 2429 span: Span,
dfeec247
XL
2430 init: Option<&'hir hir::Expr<'hir>>,
2431 pat: &'hir hir::Pat<'hir>,
0531ce1d 2432 source: hir::LocalSource,
dfeec247
XL
2433 ) -> hir::Stmt<'hir> {
2434 let local = hir::Local { attrs, hir_id: self.next_id(), init, pat, source, span, ty: None };
2435 self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local)))
7cac9316
XL
2436 }
2437
dfeec247
XL
2438 fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
2439 self.block_all(expr.span, &[], Some(expr))
a7813a04 2440 }
b039eaaf 2441
0531ce1d
XL
2442 fn block_all(
2443 &mut self,
2444 span: Span,
dfeec247
XL
2445 stmts: &'hir [hir::Stmt<'hir>],
2446 expr: Option<&'hir hir::Expr<'hir>>,
2447 ) -> &'hir hir::Block<'hir> {
2448 let blk = hir::Block {
041b39d2
XL
2449 stmts,
2450 expr,
48663c56 2451 hir_id: self.next_id(),
dfeec247 2452 rules: hir::BlockCheckMode::DefaultBlock,
041b39d2 2453 span,
cc61c64b 2454 targeted_by_break: false,
dfeec247
XL
2455 };
2456 self.arena.alloc(blk)
a7813a04 2457 }
b039eaaf 2458
48663c56 2459 /// Constructs a `true` or `false` literal pattern.
dfeec247 2460 fn pat_bool(&mut self, span: Span, val: bool) -> &'hir hir::Pat<'hir> {
dc9dc135 2461 let expr = self.expr_bool(span, val);
dfeec247 2462 self.pat(span, hir::PatKind::Lit(expr))
48663c56
XL
2463 }
2464
dfeec247 2465 fn pat_ok(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3dfed10e
XL
2466 let field = self.single_pat_field(span, pat);
2467 self.pat_lang_item_variant(span, hir::LangItem::ResultOk, field)
a7813a04 2468 }
b039eaaf 2469
dfeec247 2470 fn pat_err(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3dfed10e
XL
2471 let field = self.single_pat_field(span, pat);
2472 self.pat_lang_item_variant(span, hir::LangItem::ResultErr, field)
a7813a04 2473 }
b039eaaf 2474
dfeec247 2475 fn pat_some(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3dfed10e
XL
2476 let field = self.single_pat_field(span, pat);
2477 self.pat_lang_item_variant(span, hir::LangItem::OptionSome, field)
a7813a04 2478 }
b039eaaf 2479
dfeec247 2480 fn pat_none(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
3dfed10e 2481 self.pat_lang_item_variant(span, hir::LangItem::OptionNone, &[])
a7813a04 2482 }
b039eaaf 2483
3dfed10e 2484 fn single_pat_field(
0531ce1d
XL
2485 &mut self,
2486 span: Span,
3dfed10e
XL
2487 pat: &'hir hir::Pat<'hir>,
2488 ) -> &'hir [hir::FieldPat<'hir>] {
2489 let field = hir::FieldPat {
2490 hir_id: self.next_id(),
2491 ident: Ident::new(sym::integer(0), span),
2492 is_shorthand: false,
2493 pat,
2494 span,
a7813a04 2495 };
3dfed10e
XL
2496 arena_vec![self; field]
2497 }
2498
2499 fn pat_lang_item_variant(
2500 &mut self,
2501 span: Span,
2502 lang_item: hir::LangItem,
2503 fields: &'hir [hir::FieldPat<'hir>],
2504 ) -> &'hir hir::Pat<'hir> {
2505 let qpath = hir::QPath::LangItem(lang_item, span);
2506 self.pat(span, hir::PatKind::Struct(qpath, fields, false))
e9174d1e 2507 }
e9174d1e 2508
dfeec247 2509 fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, hir::HirId) {
8faf50e0 2510 self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated)
e9174d1e 2511 }
e9174d1e 2512
0531ce1d
XL
2513 fn pat_ident_binding_mode(
2514 &mut self,
2515 span: Span,
8faf50e0 2516 ident: Ident,
0531ce1d 2517 bm: hir::BindingAnnotation,
dfeec247 2518 ) -> (&'hir hir::Pat<'hir>, hir::HirId) {
48663c56 2519 let hir_id = self.next_id();
b039eaaf 2520
532ac7d7 2521 (
dfeec247 2522 self.arena.alloc(hir::Pat {
532ac7d7 2523 hir_id,
e74abb32 2524 kind: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
532ac7d7
XL
2525 span,
2526 }),
dfeec247 2527 hir_id,
532ac7d7 2528 )
a7813a04 2529 }
b039eaaf 2530
dfeec247 2531 fn pat_wild(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
a7813a04
XL
2532 self.pat(span, hir::PatKind::Wild)
2533 }
b039eaaf 2534
dfeec247
XL
2535 fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
2536 self.arena.alloc(hir::Pat { hir_id: self.next_id(), kind, span })
b039eaaf
SL
2537 }
2538
dfeec247
XL
2539 fn ty_path(
2540 &mut self,
2541 mut hir_id: hir::HirId,
2542 span: Span,
2543 qpath: hir::QPath<'hir>,
2544 ) -> hir::Ty<'hir> {
e74abb32 2545 let kind = match qpath {
32a655c1 2546 hir::QPath::Resolved(None, path) => {
8faf50e0 2547 // Turn trait object paths into `TyKind::TraitObject` instead.
48663c56 2548 match path.res {
ba9703b0 2549 Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
a1dfa0c6 2550 let principal = hir::PolyTraitRef {
dfeec247
XL
2551 bound_generic_params: &[],
2552 trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
a1dfa0c6
XL
2553 span,
2554 };
32a655c1 2555
a1dfa0c6
XL
2556 // The original ID is taken by the `PolyTraitRef`,
2557 // so the `Ty` itself needs a different one.
48663c56 2558 hir_id = self.next_id();
dfeec247
XL
2559 hir::TyKind::TraitObject(
2560 arena_vec![self; principal],
2561 self.elided_dyn_bound(span),
2562 )
a1dfa0c6
XL
2563 }
2564 _ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
32a655c1
SL
2565 }
2566 }
8faf50e0 2567 _ => hir::TyKind::Path(qpath),
32a655c1 2568 };
e74abb32 2569
dfeec247 2570 hir::Ty { hir_id, kind, span }
0531ce1d
XL
2571 }
2572
2573 /// Invoked to create the lifetime argument for a type `&T`
2574 /// with no explicit lifetime.
2575 fn elided_ref_lifetime(&mut self, span: Span) -> hir::Lifetime {
2576 match self.anonymous_lifetime_mode {
532ac7d7
XL
2577 // Intercept when we are in an impl header or async fn and introduce an in-band
2578 // lifetime.
0531ce1d
XL
2579 // Hence `impl Foo for &u32` becomes `impl<'f> Foo for &'f u32` for some fresh
2580 // `'f`.
2581 AnonymousLifetimeMode::CreateParameter => {
2582 let fresh_name = self.collect_fresh_in_band_lifetime(span);
2583 hir::Lifetime {
48663c56 2584 hir_id: self.next_id(),
0531ce1d 2585 span,
8faf50e0 2586 name: hir::LifetimeName::Param(fresh_name),
0531ce1d
XL
2587 }
2588 }
2589
0bf4aa26
XL
2590 AnonymousLifetimeMode::ReportError => self.new_error_lifetime(None, span),
2591
0531ce1d
XL
2592 AnonymousLifetimeMode::PassThrough => self.new_implicit_lifetime(span),
2593 }
2594 }
2595
0bf4aa26
XL
2596 /// Report an error on illegal use of `'_` or a `&T` with no explicit lifetime;
2597 /// return a "error lifetime".
2598 fn new_error_lifetime(&mut self, id: Option<NodeId>, span: Span) -> hir::Lifetime {
2599 let (id, msg, label) = match id {
2600 Some(id) => (id, "`'_` cannot be used here", "`'_` is a reserved lifetime name"),
2601
2602 None => (
60c5eb7d 2603 self.resolver.next_node_id(),
0bf4aa26
XL
2604 "`&` without an explicit lifetime name cannot be used here",
2605 "explicit lifetime name needed here",
2606 ),
2607 };
2608
dfeec247 2609 let mut err = struct_span_err!(self.sess, span, E0637, "{}", msg,);
0bf4aa26
XL
2610 err.span_label(span, label);
2611 err.emit();
2612
2613 self.new_named_lifetime(id, span, hir::LifetimeName::Error)
2614 }
2615
0531ce1d
XL
2616 /// Invoked to create the lifetime argument(s) for a path like
2617 /// `std::cell::Ref<T>`; note that implicit lifetimes in these
2618 /// sorts of cases are deprecated. This may therefore report a warning or an
2619 /// error, depending on the mode.
dfeec247
XL
2620 fn elided_path_lifetimes<'s>(
2621 &'s mut self,
2622 span: Span,
2623 count: usize,
2624 ) -> impl Iterator<Item = hir::Lifetime> + Captures<'a> + Captures<'s> + Captures<'hir> {
2625 (0..count).map(move |_| self.elided_path_lifetime(span))
532ac7d7
XL
2626 }
2627
2628 fn elided_path_lifetime(&mut self, span: Span) -> hir::Lifetime {
0531ce1d 2629 match self.anonymous_lifetime_mode {
48663c56
XL
2630 AnonymousLifetimeMode::CreateParameter => {
2631 // We should have emitted E0726 when processing this path above
dfeec247
XL
2632 self.sess
2633 .delay_span_bug(span, "expected 'implicit elided lifetime not allowed' error");
60c5eb7d 2634 let id = self.resolver.next_node_id();
48663c56
XL
2635 self.new_named_lifetime(id, span, hir::LifetimeName::Error)
2636 }
e74abb32
XL
2637 // `PassThrough` is the normal case.
2638 // `new_error_lifetime`, which would usually be used in the case of `ReportError`,
2639 // is unsuitable here, as these can occur from missing lifetime parameters in a
2640 // `PathSegment`, for which there is no associated `'_` or `&T` with no explicit
2641 // lifetime. Instead, we simply create an implicit lifetime, which will be checked
2642 // later, at which point a suitable error will be emitted.
dfeec247
XL
2643 AnonymousLifetimeMode::PassThrough | AnonymousLifetimeMode::ReportError => {
2644 self.new_implicit_lifetime(span)
2645 }
0531ce1d 2646 }
0531ce1d
XL
2647 }
2648
2649 /// Invoked to create the lifetime argument(s) for an elided trait object
2650 /// bound, like the bound in `Box<dyn Debug>`. This method is not invoked
2651 /// when the bound is written, even if it is written with `'_` like in
2652 /// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
2653 fn elided_dyn_bound(&mut self, span: Span) -> hir::Lifetime {
2654 match self.anonymous_lifetime_mode {
2655 // NB. We intentionally ignore the create-parameter mode here.
2656 // and instead "pass through" to resolve-lifetimes, which will apply
2657 // the object-lifetime-defaulting rules. Elided object lifetime defaults
2658 // do not act like other elided lifetimes. In other words, given this:
2659 //
2660 // impl Foo for Box<dyn Debug>
2661 //
2662 // we do not introduce a fresh `'_` to serve as the bound, but instead
2663 // ultimately translate to the equivalent of:
2664 //
2665 // impl Foo for Box<dyn Debug + 'static>
2666 //
2667 // `resolve_lifetime` has the code to make that happen.
2668 AnonymousLifetimeMode::CreateParameter => {}
2669
0bf4aa26
XL
2670 AnonymousLifetimeMode::ReportError => {
2671 // ReportError applies to explicit use of `'_`.
2672 }
2673
0531ce1d
XL
2674 // This is the normal case.
2675 AnonymousLifetimeMode::PassThrough => {}
2676 }
2677
e1599b0c
XL
2678 let r = hir::Lifetime {
2679 hir_id: self.next_id(),
2680 span,
2681 name: hir::LifetimeName::ImplicitObjectLifetimeDefault,
2682 };
2683 debug!("elided_dyn_bound: r={:?}", r);
2684 r
532ac7d7
XL
2685 }
2686
0531ce1d 2687 fn new_implicit_lifetime(&mut self, span: Span) -> hir::Lifetime {
dfeec247 2688 hir::Lifetime { hir_id: self.next_id(), span, name: hir::LifetimeName::Implicit }
9e0c209e 2689 }
0531ce1d 2690
e74abb32 2691 fn maybe_lint_bare_trait(&mut self, span: Span, id: NodeId, is_global: bool) {
416331ca
XL
2692 // FIXME(davidtwco): This is a hack to detect macros which produce spans of the
2693 // call site which do not have a macro backtrace. See #61963.
dfeec247
XL
2694 let is_macro_callsite = self
2695 .sess
2696 .source_map()
416331ca
XL
2697 .span_to_snippet(span)
2698 .map(|snippet| snippet.starts_with("#["))
2699 .unwrap_or(true);
2700 if !is_macro_callsite {
e74abb32 2701 self.resolver.lint_buffer().buffer_lint_with_diagnostic(
74b04a01 2702 BARE_TRAIT_OBJECTS,
416331ca 2703 id,
48663c56 2704 span,
416331ca 2705 "trait objects without an explicit `dyn` are deprecated",
dfeec247 2706 BuiltinLintDiagnostics::BareTraitObject(span, is_global),
416331ca
XL
2707 )
2708 }
48663c56 2709 }
e9174d1e 2710}
8bb4bdeb 2711
dfeec247 2712fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {
8bb4bdeb
XL
2713 // Sorting by span ensures that we get things in order within a
2714 // file, and also puts the files in a sensible order.
2715 let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
2716 body_ids.sort_by_key(|b| bodies[b].value.span);
2717 body_ids
2718}
48663c56 2719
dfeec247
XL
2720/// Helper struct for delayed construction of GenericArgs.
2721struct GenericArgsCtor<'hir> {
2722 args: SmallVec<[hir::GenericArg<'hir>; 4]>,
2723 bindings: &'hir [hir::TypeBinding<'hir>],
2724 parenthesized: bool,
2725}
48663c56 2726
dfeec247
XL
2727impl<'hir> GenericArgsCtor<'hir> {
2728 fn is_empty(&self) -> bool {
2729 self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
2730 }
48663c56 2731
dfeec247
XL
2732 fn into_generic_args(self, arena: &'hir Arena<'hir>) -> hir::GenericArgs<'hir> {
2733 hir::GenericArgs {
2734 args: arena.alloc_from_iter(self.args),
2735 bindings: self.bindings,
2736 parenthesized: self.parenthesized,
48663c56 2737 }
48663c56 2738 }
48663c56 2739}