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