]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use super::{AnonymousLifetimeMode, LoweringContext, ParamMode}; |
2 | use super::{ImplTraitContext, ImplTraitPosition, ImplTraitTypeIdVisitor}; | |
3 | ||
4 | use rustc::arena::Arena; | |
5 | use rustc::bug; | |
74b04a01 XL |
6 | use rustc_ast::ast::*; |
7 | use rustc_ast::attr; | |
8 | use rustc_ast::node_id::NodeMap; | |
9 | use rustc_ast::visit::{self, AssocCtxt, Visitor}; | |
dfeec247 XL |
10 | use rustc_errors::struct_span_err; |
11 | use rustc_hir as hir; | |
12 | use rustc_hir::def::{DefKind, Res}; | |
13 | use rustc_hir::def_id::DefId; | |
dfeec247 XL |
14 | use rustc_span::source_map::{respan, DesugaringKind}; |
15 | use rustc_span::symbol::{kw, sym}; | |
16 | use rustc_span::Span; | |
60c5eb7d | 17 | use rustc_target::spec::abi; |
416331ca | 18 | |
dfeec247 XL |
19 | use log::debug; |
20 | use smallvec::{smallvec, SmallVec}; | |
21 | use std::collections::BTreeSet; | |
60c5eb7d | 22 | |
dfeec247 XL |
23 | pub(super) struct ItemLowerer<'a, 'lowering, 'hir> { |
24 | pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>, | |
416331ca XL |
25 | } |
26 | ||
dfeec247 XL |
27 | impl ItemLowerer<'_, '_, '_> { |
28 | fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) { | |
416331ca | 29 | let old = self.lctx.is_in_trait_impl; |
dfeec247 | 30 | self.lctx.is_in_trait_impl = if let &None = impl_ref { false } else { true }; |
416331ca XL |
31 | f(self); |
32 | self.lctx.is_in_trait_impl = old; | |
33 | } | |
34 | } | |
35 | ||
dfeec247 XL |
36 | impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { |
37 | fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) { | |
e74abb32 XL |
38 | let hir_id = self.lctx.lower_node_id(n); |
39 | ||
dfeec247 XL |
40 | self.lctx.modules.insert( |
41 | hir_id, | |
42 | hir::ModuleItems { | |
43 | items: BTreeSet::new(), | |
44 | trait_items: BTreeSet::new(), | |
45 | impl_items: BTreeSet::new(), | |
46 | }, | |
47 | ); | |
416331ca XL |
48 | |
49 | let old = self.lctx.current_module; | |
e74abb32 | 50 | self.lctx.current_module = hir_id; |
416331ca XL |
51 | visit::walk_mod(self, m); |
52 | self.lctx.current_module = old; | |
53 | } | |
54 | ||
dfeec247 | 55 | fn visit_item(&mut self, item: &'a Item) { |
416331ca XL |
56 | let mut item_hir_id = None; |
57 | self.lctx.with_hir_id_owner(item.id, |lctx| { | |
e1599b0c XL |
58 | lctx.without_in_scope_lifetime_defs(|lctx| { |
59 | if let Some(hir_item) = lctx.lower_item(item) { | |
60 | item_hir_id = Some(hir_item.hir_id); | |
61 | lctx.insert_item(hir_item); | |
62 | } | |
63 | }) | |
416331ca XL |
64 | }); |
65 | ||
66 | if let Some(hir_id) = item_hir_id { | |
67 | self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { | |
68 | let this = &mut ItemLowerer { lctx: this }; | |
74b04a01 | 69 | if let ItemKind::Impl { ref of_trait, .. } = item.kind { |
dfeec247 | 70 | this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item)); |
416331ca XL |
71 | } else { |
72 | visit::walk_item(this, item); | |
73 | } | |
74 | }); | |
75 | } | |
76 | } | |
77 | ||
74b04a01 XL |
78 | fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) { |
79 | self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt { | |
80 | AssocCtxt::Trait => { | |
81 | let hir_item = lctx.lower_trait_item(item); | |
82 | let id = hir::TraitItemId { hir_id: hir_item.hir_id }; | |
83 | lctx.trait_items.insert(id, hir_item); | |
84 | lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id); | |
85 | } | |
86 | AssocCtxt::Impl => { | |
87 | let hir_item = lctx.lower_impl_item(item); | |
88 | let id = hir::ImplItemId { hir_id: hir_item.hir_id }; | |
89 | lctx.impl_items.insert(id, hir_item); | |
90 | lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id); | |
91 | } | |
416331ca XL |
92 | }); |
93 | ||
74b04a01 | 94 | visit::walk_assoc_item(self, item, ctxt); |
416331ca XL |
95 | } |
96 | } | |
97 | ||
dfeec247 | 98 | impl<'hir> LoweringContext<'_, 'hir> { |
416331ca XL |
99 | // Same as the method above, but accepts `hir::GenericParam`s |
100 | // instead of `ast::GenericParam`s. | |
101 | // This should only be used with generics that have already had their | |
102 | // in-band lifetimes added. In practice, this means that this function is | |
103 | // only used when lowering a child item of a trait or impl. | |
104 | fn with_parent_item_lifetime_defs<T>( | |
105 | &mut self, | |
106 | parent_hir_id: hir::HirId, | |
dfeec247 | 107 | f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T, |
416331ca XL |
108 | ) -> T { |
109 | let old_len = self.in_scope_lifetimes.len(); | |
110 | ||
e74abb32 | 111 | let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind { |
dfeec247 XL |
112 | hir::ItemKind::Impl { ref generics, .. } |
113 | | hir::ItemKind::Trait(_, _, ref generics, ..) => &generics.params[..], | |
416331ca XL |
114 | _ => &[], |
115 | }; | |
116 | let lt_def_names = parent_generics.iter().filter_map(|param| match param.kind { | |
e1599b0c | 117 | hir::GenericParamKind::Lifetime { .. } => Some(param.name.modern()), |
416331ca XL |
118 | _ => None, |
119 | }); | |
120 | self.in_scope_lifetimes.extend(lt_def_names); | |
121 | ||
122 | let res = f(self); | |
123 | ||
124 | self.in_scope_lifetimes.truncate(old_len); | |
125 | res | |
126 | } | |
127 | ||
e1599b0c XL |
128 | // Clears (and restores) the `in_scope_lifetimes` field. Used when |
129 | // visiting nested items, which never inherit in-scope lifetimes | |
130 | // from their surrounding environment. | |
131 | fn without_in_scope_lifetime_defs<T>( | |
132 | &mut self, | |
dfeec247 | 133 | f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T, |
e1599b0c XL |
134 | ) -> T { |
135 | let old_in_scope_lifetimes = std::mem::replace(&mut self.in_scope_lifetimes, vec![]); | |
136 | ||
137 | // this vector is only used when walking over impl headers, | |
138 | // input types, and the like, and should not be non-empty in | |
139 | // between items | |
140 | assert!(self.lifetimes_to_define.is_empty()); | |
141 | ||
142 | let res = f(self); | |
143 | ||
144 | assert!(self.in_scope_lifetimes.is_empty()); | |
145 | self.in_scope_lifetimes = old_in_scope_lifetimes; | |
146 | ||
147 | res | |
148 | } | |
149 | ||
dfeec247 | 150 | pub(super) fn lower_mod(&mut self, m: &Mod) -> hir::Mod<'hir> { |
416331ca XL |
151 | hir::Mod { |
152 | inner: m.inner, | |
dfeec247 XL |
153 | item_ids: self |
154 | .arena | |
155 | .alloc_from_iter(m.items.iter().flat_map(|x| self.lower_item_id(x))), | |
416331ca XL |
156 | } |
157 | } | |
158 | ||
159 | pub(super) fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> { | |
e74abb32 | 160 | let node_ids = match i.kind { |
416331ca XL |
161 | ItemKind::Use(ref use_tree) => { |
162 | let mut vec = smallvec![i.id]; | |
163 | self.lower_item_id_use_tree(use_tree, i.id, &mut vec); | |
164 | vec | |
165 | } | |
166 | ItemKind::MacroDef(..) => SmallVec::new(), | |
dfeec247 | 167 | ItemKind::Fn(..) | ItemKind::Impl { of_trait: None, .. } => smallvec![i.id], |
416331ca XL |
168 | ItemKind::Static(ref ty, ..) => { |
169 | let mut ids = smallvec![i.id]; | |
170 | if self.sess.features_untracked().impl_trait_in_bindings { | |
171 | let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids }; | |
172 | visitor.visit_ty(ty); | |
173 | } | |
174 | ids | |
dfeec247 | 175 | } |
74b04a01 | 176 | ItemKind::Const(_, ref ty, ..) => { |
416331ca XL |
177 | let mut ids = smallvec![i.id]; |
178 | if self.sess.features_untracked().impl_trait_in_bindings { | |
179 | let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids }; | |
180 | visitor.visit_ty(ty); | |
181 | } | |
182 | ids | |
dfeec247 | 183 | } |
416331ca XL |
184 | _ => smallvec![i.id], |
185 | }; | |
186 | ||
dfeec247 XL |
187 | node_ids |
188 | .into_iter() | |
189 | .map(|node_id| hir::ItemId { id: self.allocate_hir_id_counter(node_id) }) | |
190 | .collect() | |
416331ca XL |
191 | } |
192 | ||
193 | fn lower_item_id_use_tree( | |
194 | &mut self, | |
195 | tree: &UseTree, | |
196 | base_id: NodeId, | |
dfeec247 | 197 | vec: &mut SmallVec<[NodeId; 1]>, |
416331ca XL |
198 | ) { |
199 | match tree.kind { | |
dfeec247 XL |
200 | UseTreeKind::Nested(ref nested_vec) => { |
201 | for &(ref nested, id) in nested_vec { | |
202 | vec.push(id); | |
203 | self.lower_item_id_use_tree(nested, id, vec); | |
204 | } | |
205 | } | |
416331ca XL |
206 | UseTreeKind::Glob => {} |
207 | UseTreeKind::Simple(_, id1, id2) => { | |
dfeec247 XL |
208 | for (_, &id) in |
209 | self.expect_full_res_from_use(base_id).skip(1).zip([id1, id2].iter()) | |
416331ca XL |
210 | { |
211 | vec.push(id); | |
212 | } | |
dfeec247 | 213 | } |
416331ca XL |
214 | } |
215 | } | |
216 | ||
dfeec247 | 217 | pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> { |
416331ca XL |
218 | let mut ident = i.ident; |
219 | let mut vis = self.lower_visibility(&i.vis, None); | |
60c5eb7d | 220 | let attrs = self.lower_attrs(&i.attrs); |
416331ca | 221 | |
e74abb32 | 222 | if let ItemKind::MacroDef(ref def) = i.kind { |
416331ca | 223 | if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) { |
60c5eb7d | 224 | let body = self.lower_token_stream(def.body.inner_tokens()); |
416331ca XL |
225 | let hir_id = self.lower_node_id(i.id); |
226 | self.exported_macros.push(hir::MacroDef { | |
227 | name: ident.name, | |
228 | vis, | |
229 | attrs, | |
230 | hir_id, | |
231 | span: i.span, | |
232 | body, | |
233 | legacy: def.legacy, | |
234 | }); | |
235 | } else { | |
dfeec247 | 236 | self.non_exported_macro_attrs.extend(attrs.iter().cloned()); |
416331ca XL |
237 | } |
238 | return None; | |
239 | } | |
240 | ||
dfeec247 | 241 | let kind = self.lower_item_kind(i.span, i.id, &mut ident, attrs, &mut vis, &i.kind); |
416331ca | 242 | |
dfeec247 | 243 | Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span }) |
416331ca XL |
244 | } |
245 | ||
246 | fn lower_item_kind( | |
247 | &mut self, | |
dfeec247 | 248 | span: Span, |
416331ca XL |
249 | id: NodeId, |
250 | ident: &mut Ident, | |
dfeec247 XL |
251 | attrs: &'hir [Attribute], |
252 | vis: &mut hir::Visibility<'hir>, | |
416331ca | 253 | i: &ItemKind, |
dfeec247 | 254 | ) -> hir::ItemKind<'hir> { |
416331ca XL |
255 | match *i { |
256 | ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(orig_name), | |
257 | ItemKind::Use(ref use_tree) => { | |
258 | // Start with an empty prefix. | |
dfeec247 | 259 | let prefix = Path { segments: vec![], span: use_tree.span }; |
416331ca XL |
260 | |
261 | self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs) | |
262 | } | |
263 | ItemKind::Static(ref t, m, ref e) => { | |
74b04a01 XL |
264 | let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); |
265 | hir::ItemKind::Static(ty, m, body_id) | |
416331ca | 266 | } |
74b04a01 XL |
267 | ItemKind::Const(_, ref t, ref e) => { |
268 | let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); | |
269 | hir::ItemKind::Const(ty, body_id) | |
416331ca | 270 | } |
74b04a01 | 271 | ItemKind::Fn(_, FnSig { ref decl, header }, ref generics, ref body) => { |
416331ca XL |
272 | let fn_def_id = self.resolver.definitions().local_def_id(id); |
273 | self.with_new_scopes(|this| { | |
274 | this.current_item = Some(ident.span); | |
275 | ||
276 | // Note: we don't need to change the return type from `T` to | |
277 | // `impl Future<Output = T>` here because lower_body | |
278 | // only cares about the input argument patterns in the function | |
279 | // declaration (decl), not the return types. | |
74b04a01 | 280 | let asyncness = header.asyncness; |
dfeec247 | 281 | let body_id = |
74b04a01 | 282 | this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref()); |
416331ca | 283 | |
60c5eb7d | 284 | let (generics, decl) = this.add_in_band_defs( |
416331ca XL |
285 | generics, |
286 | fn_def_id, | |
287 | AnonymousLifetimeMode::PassThrough, | |
dfeec247 | 288 | |this, idty| { |
74b04a01 XL |
289 | let ret_id = asyncness.opt_return_id(); |
290 | this.lower_fn_decl(&decl, Some((fn_def_id, idty)), true, ret_id) | |
dfeec247 | 291 | }, |
416331ca | 292 | ); |
60c5eb7d XL |
293 | let sig = hir::FnSig { decl, header: this.lower_fn_header(header) }; |
294 | hir::ItemKind::Fn(sig, generics, body_id) | |
416331ca XL |
295 | }) |
296 | } | |
297 | ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)), | |
298 | ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)), | |
299 | ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)), | |
74b04a01 | 300 | ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => match ty.kind.opaque_top_hack() { |
60c5eb7d XL |
301 | None => { |
302 | let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); | |
74b04a01 | 303 | let generics = self.lower_generics(gen, ImplTraitContext::disallowed()); |
60c5eb7d | 304 | hir::ItemKind::TyAlias(ty, generics) |
dfeec247 | 305 | } |
60c5eb7d | 306 | Some(bounds) => { |
74b04a01 | 307 | let ctx = || ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc); |
60c5eb7d | 308 | let ty = hir::OpaqueTy { |
74b04a01 XL |
309 | generics: self.lower_generics(gen, ctx()), |
310 | bounds: self.lower_param_bounds(bounds, ctx()), | |
60c5eb7d XL |
311 | impl_trait_fn: None, |
312 | origin: hir::OpaqueTyOrigin::TypeAlias, | |
313 | }; | |
314 | hir::ItemKind::OpaqueTy(ty) | |
315 | } | |
416331ca | 316 | }, |
74b04a01 XL |
317 | ItemKind::TyAlias(_, ref generics, _, None) => { |
318 | let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err)); | |
319 | let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); | |
320 | hir::ItemKind::TyAlias(ty, generics) | |
321 | } | |
dfeec247 XL |
322 | ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemKind::Enum( |
323 | hir::EnumDef { | |
324 | variants: self.arena.alloc_from_iter( | |
325 | enum_definition.variants.iter().map(|x| self.lower_variant(x)), | |
326 | ), | |
327 | }, | |
328 | self.lower_generics(generics, ImplTraitContext::disallowed()), | |
329 | ), | |
416331ca XL |
330 | ItemKind::Struct(ref struct_def, ref generics) => { |
331 | let struct_def = self.lower_variant_data(struct_def); | |
332 | hir::ItemKind::Struct( | |
333 | struct_def, | |
334 | self.lower_generics(generics, ImplTraitContext::disallowed()), | |
335 | ) | |
336 | } | |
337 | ItemKind::Union(ref vdata, ref generics) => { | |
338 | let vdata = self.lower_variant_data(vdata); | |
339 | hir::ItemKind::Union( | |
340 | vdata, | |
341 | self.lower_generics(generics, ImplTraitContext::disallowed()), | |
342 | ) | |
343 | } | |
dfeec247 | 344 | ItemKind::Impl { |
416331ca XL |
345 | unsafety, |
346 | polarity, | |
347 | defaultness, | |
dfeec247 XL |
348 | constness, |
349 | generics: ref ast_generics, | |
350 | of_trait: ref trait_ref, | |
351 | self_ty: ref ty, | |
352 | items: ref impl_items, | |
353 | } => { | |
416331ca XL |
354 | let def_id = self.resolver.definitions().local_def_id(id); |
355 | ||
356 | // Lower the "impl header" first. This ordering is important | |
357 | // for in-band lifetimes! Consider `'a` here: | |
358 | // | |
359 | // impl Foo<'a> for u32 { | |
360 | // fn method(&'a self) { .. } | |
361 | // } | |
362 | // | |
363 | // Because we start by lowering the `Foo<'a> for u32` | |
364 | // part, we will add `'a` to the list of generics on | |
365 | // the impl. When we then encounter it later in the | |
366 | // method, it will not be considered an in-band | |
367 | // lifetime to be added, but rather a reference to a | |
368 | // parent lifetime. | |
369 | let lowered_trait_impl_id = self.lower_node_id(id); | |
370 | let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs( | |
371 | ast_generics, | |
372 | def_id, | |
373 | AnonymousLifetimeMode::CreateParameter, | |
374 | |this, _| { | |
375 | let trait_ref = trait_ref.as_ref().map(|trait_ref| { | |
376 | this.lower_trait_ref(trait_ref, ImplTraitContext::disallowed()) | |
377 | }); | |
378 | ||
379 | if let Some(ref trait_ref) = trait_ref { | |
380 | if let Res::Def(DefKind::Trait, def_id) = trait_ref.path.res { | |
dfeec247 XL |
381 | this.trait_impls |
382 | .entry(def_id) | |
383 | .or_default() | |
384 | .push(lowered_trait_impl_id); | |
416331ca XL |
385 | } |
386 | } | |
387 | ||
388 | let lowered_ty = this.lower_ty(ty, ImplTraitContext::disallowed()); | |
389 | ||
390 | (trait_ref, lowered_ty) | |
391 | }, | |
392 | ); | |
393 | ||
dfeec247 XL |
394 | let new_impl_items = |
395 | self.with_in_scope_lifetime_defs(&ast_generics.params, |this| { | |
396 | this.arena.alloc_from_iter( | |
397 | impl_items.iter().map(|item| this.lower_impl_item_ref(item)), | |
398 | ) | |
399 | }); | |
416331ca | 400 | |
dfeec247 | 401 | hir::ItemKind::Impl { |
74b04a01 | 402 | unsafety: self.lower_unsafety(unsafety), |
60c5eb7d | 403 | polarity, |
dfeec247 | 404 | defaultness: self.lower_defaultness(defaultness, true /* [1] */), |
74b04a01 | 405 | constness: self.lower_constness(constness), |
416331ca | 406 | generics, |
dfeec247 XL |
407 | of_trait: trait_ref, |
408 | self_ty: lowered_ty, | |
409 | items: new_impl_items, | |
410 | } | |
416331ca XL |
411 | } |
412 | ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => { | |
413 | let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed()); | |
dfeec247 XL |
414 | let items = self |
415 | .arena | |
416 | .alloc_from_iter(items.iter().map(|item| self.lower_trait_item_ref(item))); | |
416331ca | 417 | hir::ItemKind::Trait( |
60c5eb7d | 418 | is_auto, |
74b04a01 | 419 | self.lower_unsafety(unsafety), |
416331ca XL |
420 | self.lower_generics(generics, ImplTraitContext::disallowed()), |
421 | bounds, | |
422 | items, | |
423 | ) | |
424 | } | |
425 | ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemKind::TraitAlias( | |
426 | self.lower_generics(generics, ImplTraitContext::disallowed()), | |
427 | self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), | |
428 | ), | |
dfeec247 XL |
429 | ItemKind::MacroDef(..) | ItemKind::Mac(..) => { |
430 | bug!("`TyMac` should have been expanded by now") | |
431 | } | |
416331ca XL |
432 | } |
433 | ||
434 | // [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to | |
435 | // not cause an assertion failure inside the `lower_defaultness` function. | |
436 | } | |
437 | ||
74b04a01 XL |
438 | fn lower_const_item( |
439 | &mut self, | |
440 | ty: &Ty, | |
441 | span: Span, | |
442 | body: Option<&Expr>, | |
443 | ) -> (&'hir hir::Ty<'hir>, hir::BodyId) { | |
444 | let itctx = if self.sess.features_untracked().impl_trait_in_bindings { | |
445 | ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc) | |
446 | } else { | |
447 | ImplTraitContext::Disallowed(ImplTraitPosition::Binding) | |
448 | }; | |
449 | let ty = self.lower_ty(ty, itctx); | |
450 | (ty, self.lower_const_body(span, body)) | |
451 | } | |
452 | ||
416331ca XL |
453 | fn lower_use_tree( |
454 | &mut self, | |
455 | tree: &UseTree, | |
456 | prefix: &Path, | |
457 | id: NodeId, | |
dfeec247 | 458 | vis: &mut hir::Visibility<'hir>, |
416331ca | 459 | ident: &mut Ident, |
dfeec247 XL |
460 | attrs: &'hir [Attribute], |
461 | ) -> hir::ItemKind<'hir> { | |
416331ca XL |
462 | debug!("lower_use_tree(tree={:?})", tree); |
463 | debug!("lower_use_tree: vis = {:?}", vis); | |
464 | ||
465 | let path = &tree.prefix; | |
dfeec247 | 466 | let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect(); |
416331ca XL |
467 | |
468 | match tree.kind { | |
469 | UseTreeKind::Simple(rename, id1, id2) => { | |
470 | *ident = tree.ident(); | |
471 | ||
472 | // First, apply the prefix to the path. | |
dfeec247 | 473 | let mut path = Path { segments, span: path.span }; |
416331ca XL |
474 | |
475 | // Correctly resolve `self` imports. | |
476 | if path.segments.len() > 1 | |
477 | && path.segments.last().unwrap().ident.name == kw::SelfLower | |
478 | { | |
479 | let _ = path.segments.pop(); | |
480 | if rename.is_none() { | |
481 | *ident = path.segments.last().unwrap().ident; | |
482 | } | |
483 | } | |
484 | ||
485 | let mut resolutions = self.expect_full_res_from_use(id); | |
486 | // We want to return *something* from this function, so hold onto the first item | |
487 | // for later. | |
488 | let ret_res = self.lower_res(resolutions.next().unwrap_or(Res::Err)); | |
489 | ||
490 | // Here, we are looping over namespaces, if they exist for the definition | |
491 | // being imported. We only handle type and value namespaces because we | |
492 | // won't be dealing with macros in the rest of the compiler. | |
493 | // Essentially a single `use` which imports two names is desugared into | |
494 | // two imports. | |
495 | for (res, &new_node_id) in resolutions.zip([id1, id2].iter()) { | |
496 | let ident = *ident; | |
497 | let mut path = path.clone(); | |
498 | for seg in &mut path.segments { | |
60c5eb7d | 499 | seg.id = self.resolver.next_node_id(); |
416331ca XL |
500 | } |
501 | let span = path.span; | |
502 | ||
503 | self.with_hir_id_owner(new_node_id, |this| { | |
504 | let new_id = this.lower_node_id(new_node_id); | |
505 | let res = this.lower_res(res); | |
dfeec247 XL |
506 | let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None); |
507 | let kind = hir::ItemKind::Use(path, hir::UseKind::Single); | |
416331ca XL |
508 | let vis = this.rebuild_vis(&vis); |
509 | ||
dfeec247 XL |
510 | this.insert_item(hir::Item { |
511 | hir_id: new_id, | |
512 | ident, | |
513 | attrs, | |
514 | kind, | |
515 | vis, | |
516 | span, | |
517 | }); | |
416331ca XL |
518 | }); |
519 | } | |
520 | ||
dfeec247 | 521 | let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit, None); |
416331ca XL |
522 | hir::ItemKind::Use(path, hir::UseKind::Single) |
523 | } | |
524 | UseTreeKind::Glob => { | |
dfeec247 XL |
525 | let path = |
526 | self.lower_path(id, &Path { segments, span: path.span }, ParamMode::Explicit); | |
416331ca XL |
527 | hir::ItemKind::Use(path, hir::UseKind::Glob) |
528 | } | |
529 | UseTreeKind::Nested(ref trees) => { | |
530 | // Nested imports are desugared into simple imports. | |
531 | // So, if we start with | |
532 | // | |
533 | // ``` | |
534 | // pub(x) use foo::{a, b}; | |
535 | // ``` | |
536 | // | |
537 | // we will create three items: | |
538 | // | |
539 | // ``` | |
540 | // pub(x) use foo::a; | |
541 | // pub(x) use foo::b; | |
542 | // pub(x) use foo::{}; // <-- this is called the `ListStem` | |
543 | // ``` | |
544 | // | |
545 | // The first two are produced by recursively invoking | |
546 | // `lower_use_tree` (and indeed there may be things | |
547 | // like `use foo::{a::{b, c}}` and so forth). They | |
548 | // wind up being directly added to | |
549 | // `self.items`. However, the structure of this | |
550 | // function also requires us to return one item, and | |
551 | // for that we return the `{}` import (called the | |
552 | // `ListStem`). | |
553 | ||
dfeec247 | 554 | let prefix = Path { segments, span: prefix.span.to(path.span) }; |
416331ca XL |
555 | |
556 | // Add all the nested `PathListItem`s to the HIR. | |
557 | for &(ref use_tree, id) in trees { | |
558 | let new_hir_id = self.lower_node_id(id); | |
559 | ||
560 | let mut prefix = prefix.clone(); | |
561 | ||
562 | // Give the segments new node-ids since they are being cloned. | |
563 | for seg in &mut prefix.segments { | |
60c5eb7d | 564 | seg.id = self.resolver.next_node_id(); |
416331ca XL |
565 | } |
566 | ||
567 | // Each `use` import is an item and thus are owners of the | |
568 | // names in the path. Up to this point the nested import is | |
569 | // the current owner, since we want each desugared import to | |
570 | // own its own names, we have to adjust the owner before | |
571 | // lowering the rest of the import. | |
572 | self.with_hir_id_owner(id, |this| { | |
573 | let mut vis = this.rebuild_vis(&vis); | |
574 | let mut ident = *ident; | |
575 | ||
dfeec247 XL |
576 | let kind = |
577 | this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs); | |
578 | ||
579 | this.insert_item(hir::Item { | |
580 | hir_id: new_hir_id, | |
581 | ident, | |
582 | attrs, | |
583 | kind, | |
584 | vis, | |
585 | span: use_tree.span, | |
586 | }); | |
416331ca XL |
587 | }); |
588 | } | |
589 | ||
590 | // Subtle and a bit hacky: we lower the privacy level | |
591 | // of the list stem to "private" most of the time, but | |
592 | // not for "restricted" paths. The key thing is that | |
593 | // we don't want it to stay as `pub` (with no caveats) | |
594 | // because that affects rustdoc and also the lints | |
595 | // about `pub` items. But we can't *always* make it | |
596 | // private -- particularly not for restricted paths -- | |
597 | // because it contains node-ids that would then be | |
598 | // unused, failing the check that HirIds are "densely | |
599 | // assigned". | |
600 | match vis.node { | |
dfeec247 XL |
601 | hir::VisibilityKind::Public |
602 | | hir::VisibilityKind::Crate(_) | |
603 | | hir::VisibilityKind::Inherited => { | |
416331ca XL |
604 | *vis = respan(prefix.span.shrink_to_lo(), hir::VisibilityKind::Inherited); |
605 | } | |
606 | hir::VisibilityKind::Restricted { .. } => { | |
607 | // Do nothing here, as described in the comment on the match. | |
608 | } | |
609 | } | |
610 | ||
611 | let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err); | |
612 | let res = self.lower_res(res); | |
dfeec247 | 613 | let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit, None); |
416331ca XL |
614 | hir::ItemKind::Use(path, hir::UseKind::ListStem) |
615 | } | |
616 | } | |
617 | } | |
618 | ||
619 | /// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated | |
620 | /// many times in the HIR tree; for each occurrence, we need to assign distinct | |
621 | /// `NodeId`s. (See, e.g., #56128.) | |
dfeec247 | 622 | fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> &'hir hir::Path<'hir> { |
416331ca | 623 | debug!("rebuild_use_path(path = {:?})", path); |
dfeec247 XL |
624 | let segments = |
625 | self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment { | |
626 | ident: seg.ident, | |
627 | hir_id: seg.hir_id.map(|_| self.next_id()), | |
628 | res: seg.res, | |
629 | args: None, | |
630 | infer_args: seg.infer_args, | |
631 | })); | |
632 | self.arena.alloc(hir::Path { span: path.span, res: path.res, segments }) | |
416331ca XL |
633 | } |
634 | ||
dfeec247 | 635 | fn rebuild_vis(&mut self, vis: &hir::Visibility<'hir>) -> hir::Visibility<'hir> { |
416331ca XL |
636 | let vis_kind = match vis.node { |
637 | hir::VisibilityKind::Public => hir::VisibilityKind::Public, | |
638 | hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar), | |
639 | hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited, | |
640 | hir::VisibilityKind::Restricted { ref path, hir_id: _ } => { | |
641 | hir::VisibilityKind::Restricted { | |
dfeec247 | 642 | path: self.rebuild_use_path(path), |
416331ca XL |
643 | hir_id: self.next_id(), |
644 | } | |
645 | } | |
646 | }; | |
647 | respan(vis.span, vis_kind) | |
648 | } | |
649 | ||
dfeec247 | 650 | fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> { |
416331ca XL |
651 | let def_id = self.resolver.definitions().local_def_id(i.id); |
652 | hir::ForeignItem { | |
653 | hir_id: self.lower_node_id(i.id), | |
654 | ident: i.ident, | |
655 | attrs: self.lower_attrs(&i.attrs), | |
e74abb32 | 656 | kind: match i.kind { |
74b04a01 XL |
657 | ForeignItemKind::Fn(_, ref sig, ref generics, _) => { |
658 | let fdec = &sig.decl; | |
416331ca XL |
659 | let (generics, (fn_dec, fn_args)) = self.add_in_band_defs( |
660 | generics, | |
661 | def_id, | |
662 | AnonymousLifetimeMode::PassThrough, | |
663 | |this, _| { | |
664 | ( | |
e1599b0c | 665 | // Disallow `impl Trait` in foreign items. |
416331ca | 666 | this.lower_fn_decl(fdec, None, false, None), |
e1599b0c | 667 | this.lower_fn_params_to_names(fdec), |
416331ca XL |
668 | ) |
669 | }, | |
670 | ); | |
671 | ||
672 | hir::ForeignItemKind::Fn(fn_dec, fn_args, generics) | |
673 | } | |
74b04a01 | 674 | ForeignItemKind::Static(ref t, m, _) => { |
dfeec247 XL |
675 | let ty = self.lower_ty(t, ImplTraitContext::disallowed()); |
676 | hir::ForeignItemKind::Static(ty, m) | |
416331ca | 677 | } |
74b04a01 | 678 | ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type, |
e1599b0c | 679 | ForeignItemKind::Macro(_) => panic!("macro shouldn't exist here"), |
416331ca XL |
680 | }, |
681 | vis: self.lower_visibility(&i.vis, None), | |
682 | span: i.span, | |
683 | } | |
684 | } | |
685 | ||
dfeec247 | 686 | fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod<'hir> { |
416331ca | 687 | hir::ForeignMod { |
60c5eb7d | 688 | abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)), |
dfeec247 | 689 | items: self.arena.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item(x))), |
416331ca XL |
690 | } |
691 | } | |
692 | ||
dfeec247 XL |
693 | fn lower_global_asm(&mut self, ga: &GlobalAsm) -> &'hir hir::GlobalAsm { |
694 | self.arena.alloc(hir::GlobalAsm { asm: ga.asm }) | |
416331ca XL |
695 | } |
696 | ||
dfeec247 | 697 | fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> { |
e1599b0c XL |
698 | hir::Variant { |
699 | attrs: self.lower_attrs(&v.attrs), | |
700 | data: self.lower_variant_data(&v.data), | |
701 | disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)), | |
702 | id: self.lower_node_id(v.id), | |
703 | ident: v.ident, | |
416331ca XL |
704 | span: v.span, |
705 | } | |
706 | } | |
707 | ||
dfeec247 | 708 | fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData<'hir> { |
416331ca XL |
709 | match *vdata { |
710 | VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct( | |
dfeec247 XL |
711 | self.arena |
712 | .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))), | |
416331ca XL |
713 | recovered, |
714 | ), | |
dfeec247 XL |
715 | VariantData::Tuple(ref fields, id) => hir::VariantData::Tuple( |
716 | self.arena | |
717 | .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))), | |
718 | self.lower_node_id(id), | |
719 | ), | |
720 | VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)), | |
416331ca XL |
721 | } |
722 | } | |
723 | ||
dfeec247 | 724 | fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::StructField<'hir> { |
e74abb32 | 725 | let ty = if let TyKind::Path(ref qself, ref path) = f.ty.kind { |
416331ca XL |
726 | let t = self.lower_path_ty( |
727 | &f.ty, | |
728 | qself, | |
729 | path, | |
730 | ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124) | |
dfeec247 | 731 | ImplTraitContext::disallowed(), |
416331ca | 732 | ); |
dfeec247 | 733 | self.arena.alloc(t) |
416331ca XL |
734 | } else { |
735 | self.lower_ty(&f.ty, ImplTraitContext::disallowed()) | |
736 | }; | |
737 | hir::StructField { | |
738 | span: f.span, | |
739 | hir_id: self.lower_node_id(f.id), | |
740 | ident: match f.ident { | |
741 | Some(ident) => ident, | |
742 | // FIXME(jseyfried): positional field hygiene. | |
743 | None => Ident::new(sym::integer(index), f.span), | |
744 | }, | |
745 | vis: self.lower_visibility(&f.vis, None), | |
746 | ty, | |
747 | attrs: self.lower_attrs(&f.attrs), | |
748 | } | |
749 | } | |
750 | ||
dfeec247 | 751 | fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> { |
416331ca XL |
752 | let trait_item_def_id = self.resolver.definitions().local_def_id(i.id); |
753 | ||
e74abb32 | 754 | let (generics, kind) = match i.kind { |
74b04a01 | 755 | AssocItemKind::Const(_, ref ty, ref default) => { |
dfeec247 | 756 | let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); |
74b04a01 XL |
757 | let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); |
758 | (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body)) | |
dfeec247 | 759 | } |
74b04a01 | 760 | AssocItemKind::Fn(_, ref sig, ref generics, None) => { |
e1599b0c | 761 | let names = self.lower_fn_params_to_names(&sig.decl); |
dfeec247 | 762 | let (generics, sig) = |
74b04a01 | 763 | self.lower_method_sig(generics, sig, trait_item_def_id, false, None); |
416331ca XL |
764 | (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names))) |
765 | } | |
74b04a01 | 766 | AssocItemKind::Fn(_, ref sig, ref generics, Some(ref body)) => { |
dfeec247 XL |
767 | let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body)); |
768 | let (generics, sig) = | |
74b04a01 | 769 | self.lower_method_sig(generics, sig, trait_item_def_id, false, None); |
416331ca XL |
770 | (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id))) |
771 | } | |
74b04a01 | 772 | AssocItemKind::TyAlias(_, ref generics, ref bounds, ref default) => { |
dfeec247 | 773 | let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed())); |
74b04a01 | 774 | let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); |
e74abb32 | 775 | let kind = hir::TraitItemKind::Type( |
416331ca | 776 | self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), |
dfeec247 | 777 | ty, |
416331ca XL |
778 | ); |
779 | ||
e74abb32 | 780 | (generics, kind) |
dfeec247 XL |
781 | } |
782 | AssocItemKind::Macro(..) => bug!("macro item shouldn't exist at this point"), | |
416331ca XL |
783 | }; |
784 | ||
785 | hir::TraitItem { | |
786 | hir_id: self.lower_node_id(i.id), | |
787 | ident: i.ident, | |
788 | attrs: self.lower_attrs(&i.attrs), | |
789 | generics, | |
e74abb32 | 790 | kind, |
416331ca XL |
791 | span: i.span, |
792 | } | |
793 | } | |
794 | ||
dfeec247 | 795 | fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { |
74b04a01 XL |
796 | let (kind, has_default) = match &i.kind { |
797 | AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()), | |
798 | AssocItemKind::TyAlias(_, _, _, default) => { | |
799 | (hir::AssocItemKind::Type, default.is_some()) | |
800 | } | |
801 | AssocItemKind::Fn(_, sig, _, default) => { | |
dfeec247 | 802 | (hir::AssocItemKind::Method { has_self: sig.decl.has_self() }, default.is_some()) |
416331ca | 803 | } |
dfeec247 | 804 | AssocItemKind::Macro(..) => unimplemented!(), |
416331ca | 805 | }; |
74b04a01 XL |
806 | let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) }; |
807 | let defaultness = hir::Defaultness::Default { has_value: has_default }; | |
808 | hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind } | |
416331ca XL |
809 | } |
810 | ||
dfeec247 XL |
811 | /// Construct `ExprKind::Err` for the given `span`. |
812 | fn expr_err(&mut self, span: Span) -> hir::Expr<'hir> { | |
813 | self.expr(span, hir::ExprKind::Err, AttrVec::new()) | |
814 | } | |
815 | ||
816 | fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> { | |
416331ca XL |
817 | let impl_item_def_id = self.resolver.definitions().local_def_id(i.id); |
818 | ||
74b04a01 XL |
819 | let (generics, kind) = match &i.kind { |
820 | AssocItemKind::Const(_, ty, expr) => { | |
dfeec247 XL |
821 | let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); |
822 | ( | |
74b04a01 | 823 | hir::Generics::empty(), |
dfeec247 XL |
824 | hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())), |
825 | ) | |
826 | } | |
74b04a01 | 827 | AssocItemKind::Fn(_, sig, generics, body) => { |
416331ca | 828 | self.current_item = Some(i.span); |
74b04a01 XL |
829 | let asyncness = sig.header.asyncness; |
830 | let body_id = | |
831 | self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref()); | |
416331ca XL |
832 | let impl_trait_return_allow = !self.is_in_trait_impl; |
833 | let (generics, sig) = self.lower_method_sig( | |
74b04a01 | 834 | generics, |
416331ca XL |
835 | sig, |
836 | impl_item_def_id, | |
837 | impl_trait_return_allow, | |
74b04a01 | 838 | asyncness.opt_return_id(), |
416331ca XL |
839 | ); |
840 | ||
841 | (generics, hir::ImplItemKind::Method(sig, body_id)) | |
842 | } | |
74b04a01 XL |
843 | AssocItemKind::TyAlias(_, generics, _, ty) => { |
844 | let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); | |
dfeec247 | 845 | let kind = match ty { |
60c5eb7d | 846 | None => { |
dfeec247 | 847 | let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err)); |
60c5eb7d XL |
848 | hir::ImplItemKind::TyAlias(ty) |
849 | } | |
dfeec247 XL |
850 | Some(ty) => match ty.kind.opaque_top_hack() { |
851 | None => { | |
852 | let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); | |
853 | hir::ImplItemKind::TyAlias(ty) | |
854 | } | |
855 | Some(bs) => { | |
856 | let bs = self.lower_param_bounds(bs, ImplTraitContext::disallowed()); | |
857 | hir::ImplItemKind::OpaqueTy(bs) | |
858 | } | |
859 | }, | |
60c5eb7d XL |
860 | }; |
861 | (generics, kind) | |
dfeec247 XL |
862 | } |
863 | AssocItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"), | |
416331ca XL |
864 | }; |
865 | ||
866 | hir::ImplItem { | |
867 | hir_id: self.lower_node_id(i.id), | |
868 | ident: i.ident, | |
869 | attrs: self.lower_attrs(&i.attrs), | |
870 | generics, | |
871 | vis: self.lower_visibility(&i.vis, None), | |
74b04a01 | 872 | defaultness: self.lower_defaultness(i.kind.defaultness(), true /* [1] */), |
e74abb32 | 873 | kind, |
416331ca XL |
874 | span: i.span, |
875 | } | |
876 | ||
877 | // [1] since `default impl` is not yet implemented, this is always true in impls | |
878 | } | |
879 | ||
dfeec247 | 880 | fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef<'hir> { |
416331ca XL |
881 | hir::ImplItemRef { |
882 | id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) }, | |
883 | ident: i.ident, | |
884 | span: i.span, | |
885 | vis: self.lower_visibility(&i.vis, Some(i.id)), | |
74b04a01 | 886 | defaultness: self.lower_defaultness(i.kind.defaultness(), true /* [1] */), |
60c5eb7d | 887 | kind: match &i.kind { |
dfeec247 | 888 | AssocItemKind::Const(..) => hir::AssocItemKind::Const, |
74b04a01 | 889 | AssocItemKind::TyAlias(.., ty) => { |
dfeec247 XL |
890 | match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) { |
891 | None => hir::AssocItemKind::Type, | |
892 | Some(_) => hir::AssocItemKind::OpaqueTy, | |
893 | } | |
894 | } | |
74b04a01 | 895 | AssocItemKind::Fn(_, sig, ..) => { |
dfeec247 XL |
896 | hir::AssocItemKind::Method { has_self: sig.decl.has_self() } |
897 | } | |
898 | AssocItemKind::Macro(..) => unimplemented!(), | |
416331ca XL |
899 | }, |
900 | } | |
901 | ||
902 | // [1] since `default impl` is not yet implemented, this is always true in impls | |
903 | } | |
904 | ||
905 | /// If an `explicit_owner` is given, this method allocates the `HirId` in | |
906 | /// the address space of that item instead of the item currently being | |
907 | /// lowered. This can happen during `lower_impl_item_ref()` where we need to | |
908 | /// lower a `Visibility` value although we haven't lowered the owning | |
909 | /// `ImplItem` in question yet. | |
910 | fn lower_visibility( | |
911 | &mut self, | |
912 | v: &Visibility, | |
913 | explicit_owner: Option<NodeId>, | |
dfeec247 | 914 | ) -> hir::Visibility<'hir> { |
416331ca XL |
915 | let node = match v.node { |
916 | VisibilityKind::Public => hir::VisibilityKind::Public, | |
917 | VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar), | |
918 | VisibilityKind::Restricted { ref path, id } => { | |
919 | debug!("lower_visibility: restricted path id = {:?}", id); | |
920 | let lowered_id = if let Some(owner) = explicit_owner { | |
921 | self.lower_node_id_with_owner(id, owner) | |
922 | } else { | |
923 | self.lower_node_id(id) | |
924 | }; | |
925 | let res = self.expect_full_res(id); | |
926 | let res = self.lower_res(res); | |
927 | hir::VisibilityKind::Restricted { | |
dfeec247 | 928 | path: self.lower_path_extra(res, path, ParamMode::Explicit, explicit_owner), |
416331ca XL |
929 | hir_id: lowered_id, |
930 | } | |
dfeec247 | 931 | } |
416331ca XL |
932 | VisibilityKind::Inherited => hir::VisibilityKind::Inherited, |
933 | }; | |
934 | respan(v.span, node) | |
935 | } | |
936 | ||
937 | fn lower_defaultness(&self, d: Defaultness, has_value: bool) -> hir::Defaultness { | |
938 | match d { | |
74b04a01 | 939 | Defaultness::Default(_) => hir::Defaultness::Default { has_value }, |
416331ca XL |
940 | Defaultness::Final => { |
941 | assert!(has_value); | |
942 | hir::Defaultness::Final | |
943 | } | |
944 | } | |
945 | } | |
946 | ||
dfeec247 XL |
947 | fn record_body( |
948 | &mut self, | |
949 | params: &'hir [hir::Param<'hir>], | |
950 | value: hir::Expr<'hir>, | |
951 | ) -> hir::BodyId { | |
952 | let body = hir::Body { generator_kind: self.generator_kind, params, value }; | |
416331ca XL |
953 | let id = body.id(); |
954 | self.bodies.insert(id, body); | |
955 | id | |
956 | } | |
957 | ||
958 | fn lower_body( | |
959 | &mut self, | |
dfeec247 | 960 | f: impl FnOnce(&mut Self) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>), |
416331ca XL |
961 | ) -> hir::BodyId { |
962 | let prev_gen_kind = self.generator_kind.take(); | |
e1599b0c XL |
963 | let (parameters, result) = f(self); |
964 | let body_id = self.record_body(parameters, result); | |
416331ca XL |
965 | self.generator_kind = prev_gen_kind; |
966 | body_id | |
967 | } | |
968 | ||
dfeec247 | 969 | fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { |
e1599b0c XL |
970 | hir::Param { |
971 | attrs: self.lower_attrs(¶m.attrs), | |
972 | hir_id: self.lower_node_id(param.id), | |
973 | pat: self.lower_pat(¶m.pat), | |
974 | span: param.span, | |
416331ca XL |
975 | } |
976 | } | |
977 | ||
978 | pub(super) fn lower_fn_body( | |
979 | &mut self, | |
980 | decl: &FnDecl, | |
dfeec247 | 981 | body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, |
416331ca | 982 | ) -> hir::BodyId { |
dfeec247 XL |
983 | self.lower_body(|this| { |
984 | ( | |
985 | this.arena.alloc_from_iter(decl.inputs.iter().map(|x| this.lower_param(x))), | |
986 | body(this), | |
987 | ) | |
988 | }) | |
416331ca XL |
989 | } |
990 | ||
dfeec247 XL |
991 | fn lower_fn_body_block( |
992 | &mut self, | |
993 | span: Span, | |
994 | decl: &FnDecl, | |
995 | body: Option<&Block>, | |
996 | ) -> hir::BodyId { | |
997 | self.lower_fn_body(decl, |this| this.lower_block_expr_opt(span, body)) | |
416331ca XL |
998 | } |
999 | ||
dfeec247 XL |
1000 | fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir> { |
1001 | match block { | |
1002 | Some(block) => self.lower_block_expr(block), | |
1003 | None => self.expr_err(span), | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId { | |
1008 | self.lower_body(|this| { | |
1009 | ( | |
1010 | &[], | |
1011 | match expr { | |
1012 | Some(expr) => this.lower_expr_mut(expr), | |
1013 | None => this.expr_err(span), | |
1014 | }, | |
1015 | ) | |
1016 | }) | |
416331ca XL |
1017 | } |
1018 | ||
1019 | fn lower_maybe_async_body( | |
1020 | &mut self, | |
dfeec247 | 1021 | span: Span, |
416331ca | 1022 | decl: &FnDecl, |
74b04a01 | 1023 | asyncness: Async, |
dfeec247 | 1024 | body: Option<&Block>, |
416331ca XL |
1025 | ) -> hir::BodyId { |
1026 | let closure_id = match asyncness { | |
74b04a01 XL |
1027 | Async::Yes { closure_id, .. } => closure_id, |
1028 | Async::No => return self.lower_fn_body_block(span, decl, body), | |
416331ca XL |
1029 | }; |
1030 | ||
1031 | self.lower_body(|this| { | |
dfeec247 XL |
1032 | let mut parameters: Vec<hir::Param<'_>> = Vec::new(); |
1033 | let mut statements: Vec<hir::Stmt<'_>> = Vec::new(); | |
416331ca | 1034 | |
e1599b0c | 1035 | // Async function parameters are lowered into the closure body so that they are |
416331ca XL |
1036 | // captured and so that the drop order matches the equivalent non-async functions. |
1037 | // | |
1038 | // from: | |
1039 | // | |
1040 | // async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) { | |
e1599b0c | 1041 | // <body> |
416331ca XL |
1042 | // } |
1043 | // | |
1044 | // into: | |
1045 | // | |
1046 | // fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) { | |
1047 | // async move { | |
1048 | // let __arg2 = __arg2; | |
1049 | // let <pattern> = __arg2; | |
1050 | // let __arg1 = __arg1; | |
1051 | // let <pattern> = __arg1; | |
1052 | // let __arg0 = __arg0; | |
1053 | // let <pattern> = __arg0; | |
e1599b0c | 1054 | // drop-temps { <body> } // see comments later in fn for details |
416331ca XL |
1055 | // } |
1056 | // } | |
1057 | // | |
1058 | // If `<pattern>` is a simple ident, then it is lowered to a single | |
1059 | // `let <pattern> = <pattern>;` statement as an optimization. | |
e1599b0c XL |
1060 | // |
1061 | // Note that the body is embedded in `drop-temps`; an | |
1062 | // equivalent desugaring would be `return { <body> | |
1063 | // };`. The key point is that we wish to drop all the | |
1064 | // let-bound variables and temporaries created in the body | |
1065 | // (and its tail expression!) before we drop the | |
1066 | // parameters (c.f. rust-lang/rust#64512). | |
1067 | for (index, parameter) in decl.inputs.iter().enumerate() { | |
1068 | let parameter = this.lower_param(parameter); | |
1069 | let span = parameter.pat.span; | |
416331ca XL |
1070 | |
1071 | // Check if this is a binding pattern, if so, we can optimize and avoid adding a | |
e1599b0c | 1072 | // `let <pat> = __argN;` statement. In this case, we do not rename the parameter. |
e74abb32 | 1073 | let (ident, is_simple_parameter) = match parameter.pat.kind { |
dfeec247 XL |
1074 | hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, _) => { |
1075 | (ident, true) | |
1076 | } | |
416331ca XL |
1077 | _ => { |
1078 | // Replace the ident for bindings that aren't simple. | |
1079 | let name = format!("__arg{}", index); | |
1080 | let ident = Ident::from_str(&name); | |
1081 | ||
1082 | (ident, false) | |
dfeec247 | 1083 | } |
416331ca XL |
1084 | }; |
1085 | ||
dfeec247 | 1086 | let desugared_span = this.mark_span_with_reason(DesugaringKind::Async, span, None); |
416331ca | 1087 | |
e1599b0c | 1088 | // Construct a parameter representing `__argN: <ty>` to replace the parameter of the |
416331ca XL |
1089 | // async function. |
1090 | // | |
e1599b0c XL |
1091 | // If this is the simple case, this parameter will end up being the same as the |
1092 | // original parameter, but with a different pattern id. | |
dfeec247 | 1093 | let mut stmt_attrs = AttrVec::new(); |
e1599b0c XL |
1094 | stmt_attrs.extend(parameter.attrs.iter().cloned()); |
1095 | let (new_parameter_pat, new_parameter_id) = this.pat_ident(desugared_span, ident); | |
1096 | let new_parameter = hir::Param { | |
1097 | attrs: parameter.attrs, | |
1098 | hir_id: parameter.hir_id, | |
1099 | pat: new_parameter_pat, | |
1100 | span: parameter.span, | |
416331ca XL |
1101 | }; |
1102 | ||
e1599b0c | 1103 | if is_simple_parameter { |
416331ca XL |
1104 | // If this is the simple case, then we only insert one statement that is |
1105 | // `let <pat> = <pat>;`. We re-use the original argument's pattern so that | |
1106 | // `HirId`s are densely assigned. | |
e1599b0c | 1107 | let expr = this.expr_ident(desugared_span, ident, new_parameter_id); |
416331ca XL |
1108 | let stmt = this.stmt_let_pat( |
1109 | stmt_attrs, | |
1110 | desugared_span, | |
dfeec247 | 1111 | Some(expr), |
e1599b0c | 1112 | parameter.pat, |
dfeec247 | 1113 | hir::LocalSource::AsyncFn, |
416331ca XL |
1114 | ); |
1115 | statements.push(stmt); | |
1116 | } else { | |
1117 | // If this is not the simple case, then we construct two statements: | |
1118 | // | |
1119 | // ``` | |
1120 | // let __argN = __argN; | |
1121 | // let <pat> = __argN; | |
1122 | // ``` | |
1123 | // | |
e1599b0c | 1124 | // The first statement moves the parameter into the closure and thus ensures |
416331ca XL |
1125 | // that the drop order is correct. |
1126 | // | |
1127 | // The second statement creates the bindings that the user wrote. | |
1128 | ||
1129 | // Construct the `let mut __argN = __argN;` statement. It must be a mut binding | |
1130 | // because the user may have specified a `ref mut` binding in the next | |
1131 | // statement. | |
1132 | let (move_pat, move_id) = this.pat_ident_binding_mode( | |
dfeec247 XL |
1133 | desugared_span, |
1134 | ident, | |
1135 | hir::BindingAnnotation::Mutable, | |
1136 | ); | |
e1599b0c | 1137 | let move_expr = this.expr_ident(desugared_span, ident, new_parameter_id); |
416331ca | 1138 | let move_stmt = this.stmt_let_pat( |
dfeec247 | 1139 | AttrVec::new(), |
416331ca | 1140 | desugared_span, |
dfeec247 | 1141 | Some(move_expr), |
416331ca | 1142 | move_pat, |
dfeec247 | 1143 | hir::LocalSource::AsyncFn, |
416331ca XL |
1144 | ); |
1145 | ||
1146 | // Construct the `let <pat> = __argN;` statement. We re-use the original | |
e1599b0c | 1147 | // parameter's pattern so that `HirId`s are densely assigned. |
416331ca XL |
1148 | let pattern_expr = this.expr_ident(desugared_span, ident, move_id); |
1149 | let pattern_stmt = this.stmt_let_pat( | |
1150 | stmt_attrs, | |
1151 | desugared_span, | |
dfeec247 | 1152 | Some(pattern_expr), |
e1599b0c | 1153 | parameter.pat, |
dfeec247 | 1154 | hir::LocalSource::AsyncFn, |
416331ca XL |
1155 | ); |
1156 | ||
1157 | statements.push(move_stmt); | |
1158 | statements.push(pattern_stmt); | |
1159 | }; | |
1160 | ||
e1599b0c | 1161 | parameters.push(new_parameter); |
416331ca XL |
1162 | } |
1163 | ||
dfeec247 | 1164 | let body_span = body.map_or(span, |b| b.span); |
416331ca | 1165 | let async_expr = this.make_async_expr( |
e74abb32 XL |
1166 | CaptureBy::Value, |
1167 | closure_id, | |
1168 | None, | |
dfeec247 | 1169 | body_span, |
e74abb32 | 1170 | hir::AsyncGeneratorKind::Fn, |
416331ca | 1171 | |this| { |
e1599b0c | 1172 | // Create a block from the user's function body: |
dfeec247 | 1173 | let user_body = this.lower_block_expr_opt(body_span, body); |
e1599b0c XL |
1174 | |
1175 | // Transform into `drop-temps { <user-body> }`, an expression: | |
dfeec247 XL |
1176 | let desugared_span = |
1177 | this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None); | |
e1599b0c XL |
1178 | let user_body = this.expr_drop_temps( |
1179 | desugared_span, | |
dfeec247 XL |
1180 | this.arena.alloc(user_body), |
1181 | AttrVec::new(), | |
e1599b0c XL |
1182 | ); |
1183 | ||
1184 | // As noted above, create the final block like | |
1185 | // | |
1186 | // ``` | |
1187 | // { | |
1188 | // let $param_pattern = $raw_param; | |
1189 | // ... | |
1190 | // drop-temps { <user-body> } | |
1191 | // } | |
1192 | // ``` | |
1193 | let body = this.block_all( | |
1194 | desugared_span, | |
dfeec247 XL |
1195 | this.arena.alloc_from_iter(statements), |
1196 | Some(user_body), | |
e1599b0c | 1197 | ); |
dfeec247 XL |
1198 | |
1199 | this.expr_block(body, AttrVec::new()) | |
1200 | }, | |
1201 | ); | |
1202 | ||
1203 | ( | |
1204 | this.arena.alloc_from_iter(parameters), | |
1205 | this.expr(body_span, async_expr, AttrVec::new()), | |
1206 | ) | |
416331ca XL |
1207 | }) |
1208 | } | |
1209 | ||
1210 | fn lower_method_sig( | |
1211 | &mut self, | |
1212 | generics: &Generics, | |
60c5eb7d | 1213 | sig: &FnSig, |
416331ca XL |
1214 | fn_def_id: DefId, |
1215 | impl_trait_return_allow: bool, | |
1216 | is_async: Option<NodeId>, | |
dfeec247 | 1217 | ) -> (hir::Generics<'hir>, hir::FnSig<'hir>) { |
416331ca XL |
1218 | let header = self.lower_fn_header(sig.header); |
1219 | let (generics, decl) = self.add_in_band_defs( | |
1220 | generics, | |
1221 | fn_def_id, | |
1222 | AnonymousLifetimeMode::PassThrough, | |
dfeec247 XL |
1223 | |this, idty| { |
1224 | this.lower_fn_decl( | |
1225 | &sig.decl, | |
1226 | Some((fn_def_id, idty)), | |
1227 | impl_trait_return_allow, | |
1228 | is_async, | |
1229 | ) | |
1230 | }, | |
416331ca | 1231 | ); |
60c5eb7d | 1232 | (generics, hir::FnSig { header, decl }) |
416331ca XL |
1233 | } |
1234 | ||
1235 | fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader { | |
1236 | hir::FnHeader { | |
74b04a01 XL |
1237 | unsafety: self.lower_unsafety(h.unsafety), |
1238 | asyncness: self.lower_asyncness(h.asyncness), | |
1239 | constness: self.lower_constness(h.constness), | |
60c5eb7d | 1240 | abi: self.lower_extern(h.ext), |
416331ca XL |
1241 | } |
1242 | } | |
1243 | ||
60c5eb7d XL |
1244 | pub(super) fn lower_abi(&mut self, abi: StrLit) -> abi::Abi { |
1245 | abi::lookup(&abi.symbol_unescaped.as_str()).unwrap_or_else(|| { | |
1246 | self.error_on_invalid_abi(abi); | |
1247 | abi::Abi::Rust | |
1248 | }) | |
416331ca XL |
1249 | } |
1250 | ||
60c5eb7d XL |
1251 | pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi { |
1252 | match ext { | |
1253 | Extern::None => abi::Abi::Rust, | |
1254 | Extern::Implicit => abi::Abi::C, | |
1255 | Extern::Explicit(abi) => self.lower_abi(abi), | |
416331ca XL |
1256 | } |
1257 | } | |
1258 | ||
60c5eb7d | 1259 | fn error_on_invalid_abi(&self, abi: StrLit) { |
dfeec247 XL |
1260 | struct_span_err!(self.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol) |
1261 | .span_label(abi.span, "invalid ABI") | |
1262 | .help(&format!("valid ABIs: {}", abi::all_names().join(", "))) | |
1263 | .emit(); | |
60c5eb7d XL |
1264 | } |
1265 | ||
74b04a01 | 1266 | fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync { |
416331ca | 1267 | match a { |
74b04a01 XL |
1268 | Async::Yes { .. } => hir::IsAsync::Async, |
1269 | Async::No => hir::IsAsync::NotAsync, | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | fn lower_constness(&mut self, c: Const) -> hir::Constness { | |
1274 | match c { | |
1275 | Const::Yes(_) => hir::Constness::Const, | |
1276 | Const::No => hir::Constness::NotConst, | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety { | |
1281 | match u { | |
1282 | Unsafe::Yes(_) => hir::Unsafety::Unsafe, | |
1283 | Unsafe::No => hir::Unsafety::Normal, | |
416331ca XL |
1284 | } |
1285 | } | |
1286 | ||
dfeec247 | 1287 | pub(super) fn lower_generics_mut( |
416331ca XL |
1288 | &mut self, |
1289 | generics: &Generics, | |
dfeec247 XL |
1290 | itctx: ImplTraitContext<'_, 'hir>, |
1291 | ) -> GenericsCtor<'hir> { | |
416331ca XL |
1292 | // Collect `?Trait` bounds in where clause and move them to parameter definitions. |
1293 | // FIXME: this could probably be done with less rightward drift. It also looks like two | |
1294 | // control paths where `report_error` is called are the only paths that advance to after the | |
1295 | // match statement, so the error reporting could probably just be moved there. | |
1296 | let mut add_bounds: NodeMap<Vec<_>> = Default::default(); | |
1297 | for pred in &generics.where_clause.predicates { | |
1298 | if let WherePredicate::BoundPredicate(ref bound_pred) = *pred { | |
1299 | 'next_bound: for bound in &bound_pred.bounds { | |
1300 | if let GenericBound::Trait(_, TraitBoundModifier::Maybe) = *bound { | |
1301 | let report_error = |this: &mut Self| { | |
1302 | this.diagnostic().span_err( | |
1303 | bound_pred.bounded_ty.span, | |
1304 | "`?Trait` bounds are only permitted at the \ | |
1305 | point where a type parameter is declared", | |
1306 | ); | |
1307 | }; | |
1308 | // Check if the where clause type is a plain type parameter. | |
e74abb32 | 1309 | match bound_pred.bounded_ty.kind { |
416331ca XL |
1310 | TyKind::Path(None, ref path) |
1311 | if path.segments.len() == 1 | |
1312 | && bound_pred.bound_generic_params.is_empty() => | |
1313 | { | |
dfeec247 XL |
1314 | if let Some(Res::Def(DefKind::TyParam, def_id)) = self |
1315 | .resolver | |
416331ca XL |
1316 | .get_partial_res(bound_pred.bounded_ty.id) |
1317 | .map(|d| d.base_res()) | |
1318 | { | |
1319 | if let Some(node_id) = | |
1320 | self.resolver.definitions().as_local_node_id(def_id) | |
1321 | { | |
1322 | for param in &generics.params { | |
1323 | match param.kind { | |
1324 | GenericParamKind::Type { .. } => { | |
1325 | if node_id == param.id { | |
dfeec247 XL |
1326 | add_bounds |
1327 | .entry(param.id) | |
416331ca XL |
1328 | .or_default() |
1329 | .push(bound.clone()); | |
1330 | continue 'next_bound; | |
1331 | } | |
1332 | } | |
1333 | _ => {} | |
1334 | } | |
1335 | } | |
1336 | } | |
1337 | } | |
1338 | report_error(self) | |
1339 | } | |
1340 | _ => report_error(self), | |
1341 | } | |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | } | |
1346 | ||
dfeec247 XL |
1347 | GenericsCtor { |
1348 | params: self.lower_generic_params_mut(&generics.params, &add_bounds, itctx).collect(), | |
416331ca XL |
1349 | where_clause: self.lower_where_clause(&generics.where_clause), |
1350 | span: generics.span, | |
1351 | } | |
1352 | } | |
1353 | ||
dfeec247 XL |
1354 | pub(super) fn lower_generics( |
1355 | &mut self, | |
1356 | generics: &Generics, | |
1357 | itctx: ImplTraitContext<'_, 'hir>, | |
1358 | ) -> hir::Generics<'hir> { | |
1359 | let generics_ctor = self.lower_generics_mut(generics, itctx); | |
1360 | generics_ctor.into_generics(self.arena) | |
1361 | } | |
1362 | ||
1363 | fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause<'hir> { | |
1364 | self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| { | |
1365 | hir::WhereClause { | |
1366 | predicates: this.arena.alloc_from_iter( | |
1367 | wc.predicates.iter().map(|predicate| this.lower_where_predicate(predicate)), | |
1368 | ), | |
1369 | span: wc.span, | |
1370 | } | |
1371 | }) | |
416331ca XL |
1372 | } |
1373 | ||
dfeec247 | 1374 | fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> { |
416331ca XL |
1375 | match *pred { |
1376 | WherePredicate::BoundPredicate(WhereBoundPredicate { | |
1377 | ref bound_generic_params, | |
1378 | ref bounded_ty, | |
1379 | ref bounds, | |
1380 | span, | |
1381 | }) => { | |
dfeec247 XL |
1382 | self.with_in_scope_lifetime_defs(&bound_generic_params, |this| { |
1383 | hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { | |
1384 | bound_generic_params: this.lower_generic_params( | |
1385 | bound_generic_params, | |
1386 | &NodeMap::default(), | |
1387 | ImplTraitContext::disallowed(), | |
1388 | ), | |
1389 | bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()), | |
1390 | bounds: this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| { | |
1391 | match *bound { | |
1392 | // Ignore `?Trait` bounds. | |
1393 | // They were copied into type parameters already. | |
1394 | GenericBound::Trait(_, TraitBoundModifier::Maybe) => None, | |
1395 | _ => Some( | |
1396 | this.lower_param_bound(bound, ImplTraitContext::disallowed()), | |
1397 | ), | |
1398 | } | |
1399 | })), | |
1400 | span, | |
1401 | }) | |
1402 | }) | |
416331ca XL |
1403 | } |
1404 | WherePredicate::RegionPredicate(WhereRegionPredicate { | |
1405 | ref lifetime, | |
1406 | ref bounds, | |
1407 | span, | |
1408 | }) => hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate { | |
1409 | span, | |
1410 | lifetime: self.lower_lifetime(lifetime), | |
1411 | bounds: self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), | |
1412 | }), | |
dfeec247 | 1413 | WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => { |
416331ca XL |
1414 | hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { |
1415 | hir_id: self.lower_node_id(id), | |
1416 | lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()), | |
1417 | rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()), | |
1418 | span, | |
1419 | }) | |
dfeec247 XL |
1420 | } |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /// Helper struct for delayed construction of Generics. | |
1426 | pub(super) struct GenericsCtor<'hir> { | |
1427 | pub(super) params: SmallVec<[hir::GenericParam<'hir>; 4]>, | |
1428 | where_clause: hir::WhereClause<'hir>, | |
1429 | span: Span, | |
1430 | } | |
1431 | ||
1432 | impl<'hir> GenericsCtor<'hir> { | |
1433 | pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> { | |
1434 | hir::Generics { | |
1435 | params: arena.alloc_from_iter(self.params), | |
1436 | where_clause: self.where_clause, | |
1437 | span: self.span, | |
416331ca XL |
1438 | } |
1439 | } | |
1440 | } |