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