]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | //! A bunch of methods and structures more or less related to resolving macros and |
2 | //! interface provided by `Resolver` to macro expander. | |
3 | ||
dfeec247 | 4 | use crate::imports::ImportResolver; |
9fa01778 | 5 | use crate::Namespace::*; |
dfeec247 XL |
6 | use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy}; |
7 | use crate::{CrateLint, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak}; | |
8 | use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding}; | |
416331ca | 9 | use rustc::middle::stability; |
dfeec247 | 10 | use rustc::session::parse::feature_err; |
60c5eb7d | 11 | use rustc::session::Session; |
dfeec247 XL |
12 | use rustc::{lint, span_bug, ty}; |
13 | use rustc_data_structures::fx::FxHashSet; | |
14 | use rustc_expand::base::SyntaxExtension; | |
15 | use rustc_expand::base::{self, Indeterminate, InvocationRes}; | |
16 | use rustc_expand::compile_declarative_macro; | |
17 | use rustc_expand::expand::{AstFragment, AstFragmentKind, Invocation, InvocationKind}; | |
60c5eb7d | 18 | use rustc_feature::is_builtin_attr_name; |
dfeec247 XL |
19 | use rustc_hir::def::{self, DefKind, NonMacroAttrKind}; |
20 | use rustc_hir::def_id; | |
21 | use rustc_span::edition::Edition; | |
22 | use rustc_span::hygiene::{self, ExpnData, ExpnId, ExpnKind}; | |
23 | use rustc_span::symbol::{kw, sym, Symbol}; | |
24 | use rustc_span::{Span, DUMMY_SP}; | |
25 | use syntax::ast::{self, Ident, NodeId}; | |
60c5eb7d | 26 | use syntax::attr::{self, StabilityLevel}; |
e74abb32 | 27 | use syntax::print::pprust; |
9e0c209e | 28 | |
0531ce1d | 29 | use rustc_data_structures::sync::Lrc; |
dfeec247 XL |
30 | use rustc_span::hygiene::{AstPass, MacroKind}; |
31 | use std::{mem, ptr}; | |
b7449926 | 32 | |
e1599b0c | 33 | type Res = def::Res<NodeId>; |
c30ab7b3 | 34 | |
b7449926 XL |
35 | /// Binding produced by a `macro_rules` item. |
36 | /// Not modularized, can shadow previous legacy bindings, etc. | |
0bf4aa26 | 37 | #[derive(Debug)] |
b7449926 | 38 | pub struct LegacyBinding<'a> { |
416331ca | 39 | crate binding: &'a NameBinding<'a>, |
b7449926 | 40 | /// Legacy scope into which the `macro_rules` item was planted. |
416331ca XL |
41 | crate parent_legacy_scope: LegacyScope<'a>, |
42 | crate ident: Ident, | |
b7449926 XL |
43 | } |
44 | ||
9fa01778 XL |
45 | /// The scope introduced by a `macro_rules!` macro. |
46 | /// This starts at the macro's definition and ends at the end of the macro's parent | |
47 | /// module (named or unnamed), or even further if it escapes with `#[macro_use]`. | |
b7449926 | 48 | /// Some macro invocations need to introduce legacy scopes too because they |
9fa01778 | 49 | /// can potentially expand into macro definitions. |
0bf4aa26 | 50 | #[derive(Copy, Clone, Debug)] |
c30ab7b3 | 51 | pub enum LegacyScope<'a> { |
b7449926 | 52 | /// Empty "root" scope at the crate start containing no names. |
c30ab7b3 | 53 | Empty, |
9fa01778 | 54 | /// The scope introduced by a `macro_rules!` macro definition. |
c30ab7b3 | 55 | Binding(&'a LegacyBinding<'a>), |
9fa01778 | 56 | /// The scope introduced by a macro invocation that can potentially |
b7449926 | 57 | /// create a `macro_rules!` macro definition. |
e1599b0c | 58 | Invocation(ExpnId), |
c30ab7b3 SL |
59 | } |
60 | ||
b7449926 XL |
61 | // Macro namespace is separated into two sub-namespaces, one for bang macros and |
62 | // one for attribute-like macros (attributes, derives). | |
63 | // We ignore resolutions from one sub-namespace when searching names in scope for another. | |
13cf67c4 | 64 | fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool { |
b7449926 | 65 | #[derive(PartialEq)] |
dfeec247 XL |
66 | enum SubNS { |
67 | Bang, | |
68 | AttrLike, | |
69 | } | |
b7449926 | 70 | let sub_ns = |kind| match kind { |
416331ca XL |
71 | MacroKind::Bang => SubNS::Bang, |
72 | MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike, | |
b7449926 | 73 | }; |
416331ca XL |
74 | let candidate = candidate.map(sub_ns); |
75 | let requirement = requirement.map(sub_ns); | |
b7449926 | 76 | // "No specific sub-namespace" means "matches anything" for both requirements and candidates. |
13cf67c4 | 77 | candidate.is_none() || requirement.is_none() || candidate == requirement |
cc61c64b XL |
78 | } |
79 | ||
dc9dc135 XL |
80 | // We don't want to format a path using pretty-printing, |
81 | // `format!("{}", path)`, because that tries to insert | |
82 | // line-breaks and is slow. | |
416331ca XL |
83 | fn fast_print_path(path: &ast::Path) -> Symbol { |
84 | if path.segments.len() == 1 { | |
dfeec247 | 85 | return path.segments[0].ident.name; |
416331ca XL |
86 | } else { |
87 | let mut path_str = String::with_capacity(64); | |
88 | for (i, segment) in path.segments.iter().enumerate() { | |
89 | if i != 0 { | |
90 | path_str.push_str("::"); | |
91 | } | |
92 | if segment.ident.name != kw::PathRoot { | |
93 | path_str.push_str(&segment.ident.as_str()) | |
94 | } | |
dc9dc135 | 95 | } |
416331ca | 96 | Symbol::intern(&path_str) |
dc9dc135 | 97 | } |
dc9dc135 XL |
98 | } |
99 | ||
60c5eb7d XL |
100 | /// The code common between processing `#![register_tool]` and `#![register_attr]`. |
101 | fn registered_idents( | |
102 | sess: &Session, | |
103 | attrs: &[ast::Attribute], | |
104 | attr_name: Symbol, | |
105 | descr: &str, | |
106 | ) -> FxHashSet<Ident> { | |
107 | let mut registered = FxHashSet::default(); | |
108 | for attr in attr::filter_by_name(attrs, attr_name) { | |
109 | for nested_meta in attr.meta_item_list().unwrap_or_default() { | |
110 | match nested_meta.ident() { | |
dfeec247 XL |
111 | Some(ident) => { |
112 | if let Some(old_ident) = registered.replace(ident) { | |
113 | let msg = format!("{} `{}` was already registered", descr, ident); | |
114 | sess.struct_span_err(ident.span, &msg) | |
115 | .span_label(old_ident.span, "already registered here") | |
116 | .emit(); | |
117 | } | |
60c5eb7d XL |
118 | } |
119 | None => { | |
120 | let msg = format!("`{}` only accepts identifiers", attr_name); | |
121 | let span = nested_meta.span(); | |
122 | sess.struct_span_err(span, &msg).span_label(span, "not an identifier").emit(); | |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | registered | |
128 | } | |
129 | ||
130 | crate fn registered_attrs_and_tools( | |
131 | sess: &Session, | |
132 | attrs: &[ast::Attribute], | |
133 | ) -> (FxHashSet<Ident>, FxHashSet<Ident>) { | |
134 | let registered_attrs = registered_idents(sess, attrs, sym::register_attr, "attribute"); | |
135 | let mut registered_tools = registered_idents(sess, attrs, sym::register_tool, "tool"); | |
136 | // We implicitly add `rustfmt` and `clippy` to known tools, | |
137 | // but it's not an error to register them explicitly. | |
138 | let predefined_tools = [sym::clippy, sym::rustfmt]; | |
139 | registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span)); | |
140 | (registered_attrs, registered_tools) | |
141 | } | |
142 | ||
0731742a | 143 | impl<'a> base::Resolver for Resolver<'a> { |
e1599b0c | 144 | fn next_node_id(&mut self) -> NodeId { |
60c5eb7d | 145 | self.next_node_id() |
9e0c209e SL |
146 | } |
147 | ||
416331ca XL |
148 | fn resolve_dollar_crates(&mut self) { |
149 | hygiene::update_dollar_crate_names(|ctxt| { | |
150 | let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt)); | |
151 | match self.resolve_crate_root(ident).kind { | |
152 | ModuleKind::Def(.., name) if name != kw::Invalid => name, | |
153 | _ => kw::Crate, | |
476ff2be | 154 | } |
416331ca | 155 | }); |
32a655c1 SL |
156 | } |
157 | ||
e74abb32 | 158 | fn visit_ast_fragment_with_placeholders(&mut self, expansion: ExpnId, fragment: &AstFragment) { |
e1599b0c XL |
159 | // Integrate the new AST fragment into all the definition and module structures. |
160 | // We are inside the `expansion` now, but other parent scope components are still the same. | |
161 | let parent_scope = ParentScope { expansion, ..self.invocation_parent_scopes[&expansion] }; | |
e74abb32 | 162 | let output_legacy_scope = self.build_reduced_graph(fragment, parent_scope); |
e1599b0c XL |
163 | self.output_legacy_scopes.insert(expansion, output_legacy_scope); |
164 | ||
165 | parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion); | |
9e0c209e SL |
166 | } |
167 | ||
416331ca XL |
168 | fn register_builtin_macro(&mut self, ident: ast::Ident, ext: SyntaxExtension) { |
169 | if self.builtin_macros.insert(ident.name, ext).is_some() { | |
dfeec247 XL |
170 | self.session |
171 | .span_err(ident.span, &format!("built-in macro `{}` was already defined", ident)); | |
b7449926 | 172 | } |
9e0c209e SL |
173 | } |
174 | ||
e1599b0c XL |
175 | // Create a new Expansion with a definition site of the provided module, or |
176 | // a fake empty `#[no_implicit_prelude]` module if no module is provided. | |
177 | fn expansion_for_ast_pass( | |
178 | &mut self, | |
179 | call_site: Span, | |
180 | pass: AstPass, | |
181 | features: &[Symbol], | |
182 | parent_module_id: Option<NodeId>, | |
183 | ) -> ExpnId { | |
184 | let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable( | |
185 | ExpnKind::AstPass(pass), | |
186 | call_site, | |
187 | self.session.edition(), | |
188 | features.into(), | |
189 | ))); | |
190 | ||
191 | let parent_scope = if let Some(module_id) = parent_module_id { | |
192 | let parent_def_id = self.definitions.local_def_id(module_id); | |
193 | self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id); | |
194 | self.module_map[&parent_def_id] | |
195 | } else { | |
196 | self.definitions.add_parent_module_of_macro_def( | |
197 | expn_id, | |
198 | def_id::DefId::local(def_id::CRATE_DEF_INDEX), | |
199 | ); | |
200 | self.empty_module | |
201 | }; | |
202 | self.ast_transform_scopes.insert(expn_id, parent_scope); | |
203 | expn_id | |
204 | } | |
205 | ||
476ff2be | 206 | fn resolve_imports(&mut self) { |
416331ca | 207 | ImportResolver { r: self }.resolve_imports() |
476ff2be SL |
208 | } |
209 | ||
e1599b0c | 210 | fn resolve_macro_invocation( |
dfeec247 XL |
211 | &mut self, |
212 | invoc: &Invocation, | |
213 | eager_expansion_root: ExpnId, | |
214 | force: bool, | |
e1599b0c | 215 | ) -> Result<InvocationRes, Indeterminate> { |
416331ca | 216 | let invoc_id = invoc.expansion_data.id; |
e1599b0c XL |
217 | let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) { |
218 | Some(parent_scope) => *parent_scope, | |
219 | None => { | |
220 | // If there's no entry in the table, then we are resolving an eagerly expanded | |
221 | // macro, which should inherit its parent scope from its eager expansion root - | |
222 | // the macro that requested this eager expansion. | |
dfeec247 XL |
223 | let parent_scope = *self |
224 | .invocation_parent_scopes | |
225 | .get(&eager_expansion_root) | |
e1599b0c XL |
226 | .expect("non-eager expansion without a parent scope"); |
227 | self.invocation_parent_scopes.insert(invoc_id, parent_scope); | |
228 | parent_scope | |
229 | } | |
230 | }; | |
231 | ||
232 | let (path, kind, derives, after_derive) = match invoc.kind { | |
dfeec247 XL |
233 | InvocationKind::Attr { ref attr, ref derives, after_derive, .. } => ( |
234 | &attr.get_normal_item().path, | |
235 | MacroKind::Attr, | |
236 | self.arenas.alloc_ast_paths(derives), | |
237 | after_derive, | |
238 | ), | |
239 | InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang, &[][..], false), | |
240 | InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive, &[][..], false), | |
416331ca | 241 | InvocationKind::DeriveContainer { ref derives, .. } => { |
e1599b0c XL |
242 | // Block expansion of the container until we resolve all derives in it. |
243 | // This is required for two reasons: | |
244 | // - Derive helper attributes are in scope for the item to which the `#[derive]` | |
245 | // is applied, so they have to be produced by the container's expansion rather | |
246 | // than by individual derives. | |
247 | // - Derives in the container need to know whether one of them is a built-in `Copy`. | |
248 | // FIXME: Try to avoid repeated resolutions for derives here and in expansion. | |
249 | let mut exts = Vec::new(); | |
60c5eb7d | 250 | let mut helper_attrs = Vec::new(); |
e1599b0c | 251 | for path in derives { |
dfeec247 XL |
252 | exts.push( |
253 | match self.resolve_macro_path( | |
254 | path, | |
255 | Some(MacroKind::Derive), | |
256 | &parent_scope, | |
257 | true, | |
258 | force, | |
259 | ) { | |
260 | Ok((Some(ext), _)) => { | |
261 | let span = path.segments.last().unwrap().ident.span.modern(); | |
262 | helper_attrs.extend( | |
263 | ext.helper_attrs.iter().map(|name| Ident::new(*name, span)), | |
264 | ); | |
265 | if ext.is_derive_copy { | |
266 | self.add_derive_copy(invoc_id); | |
267 | } | |
268 | ext | |
60c5eb7d | 269 | } |
dfeec247 XL |
270 | Ok(_) | Err(Determinacy::Determined) => { |
271 | self.dummy_ext(MacroKind::Derive) | |
272 | } | |
273 | Err(Determinacy::Undetermined) => return Err(Indeterminate), | |
274 | }, | |
275 | ) | |
416331ca | 276 | } |
60c5eb7d | 277 | self.helper_attrs.insert(invoc_id, helper_attrs); |
e1599b0c | 278 | return Ok(InvocationRes::DeriveContainer(exts)); |
416331ca | 279 | } |
8bb4bdeb | 280 | }; |
b7449926 | 281 | |
e1599b0c XL |
282 | // Derives are not included when `invocations` are collected, so we have to add them here. |
283 | let parent_scope = &ParentScope { derives, ..parent_scope }; | |
416331ca | 284 | let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?; |
b7449926 | 285 | |
416331ca | 286 | let span = invoc.span(); |
e1599b0c | 287 | invoc_id.set_expn_data(ext.expn_data(parent_scope.expansion, span, fast_print_path(path))); |
dc9dc135 XL |
288 | |
289 | if let Res::Def(_, def_id) = res { | |
0bf4aa26 | 290 | if after_derive { |
416331ca | 291 | self.session.span_err(span, "macro attributes must be placed before `#[derive]`"); |
b7449926 | 292 | } |
416331ca XL |
293 | self.macro_defs.insert(invoc_id, def_id); |
294 | let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id; | |
295 | self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id); | |
8faf50e0 | 296 | } |
b7449926 | 297 | |
e1599b0c XL |
298 | match invoc.fragment_kind { |
299 | AstFragmentKind::Arms | |
dfeec247 XL |
300 | | AstFragmentKind::Fields |
301 | | AstFragmentKind::FieldPats | |
302 | | AstFragmentKind::GenericParams | |
303 | | AstFragmentKind::Params | |
304 | | AstFragmentKind::StructFields | |
305 | | AstFragmentKind::Variants => { | |
e1599b0c XL |
306 | if let Res::Def(..) = res { |
307 | self.session.span_err( | |
308 | span, | |
dfeec247 XL |
309 | &format!( |
310 | "expected an inert attribute, found {} {}", | |
311 | res.article(), | |
312 | res.descr() | |
313 | ), | |
e1599b0c XL |
314 | ); |
315 | return Ok(InvocationRes::Single(self.dummy_ext(kind))); | |
316 | } | |
dfeec247 | 317 | } |
e1599b0c XL |
318 | _ => {} |
319 | } | |
320 | ||
321 | Ok(InvocationRes::Single(ext)) | |
8bb4bdeb XL |
322 | } |
323 | ||
e74abb32 | 324 | fn check_unused_macros(&mut self) { |
416331ca | 325 | for (&node_id, &span) in self.unused_macros.iter() { |
e74abb32 | 326 | self.lint_buffer.buffer_lint( |
dfeec247 XL |
327 | lint::builtin::UNUSED_MACROS, |
328 | node_id, | |
329 | span, | |
330 | "unused macro definition", | |
416331ca | 331 | ); |
7cac9316 | 332 | } |
8bb4bdeb | 333 | } |
416331ca | 334 | |
60c5eb7d XL |
335 | fn has_derive_copy(&self, expn_id: ExpnId) -> bool { |
336 | self.containers_deriving_copy.contains(&expn_id) | |
416331ca XL |
337 | } |
338 | ||
60c5eb7d XL |
339 | fn add_derive_copy(&mut self, expn_id: ExpnId) { |
340 | self.containers_deriving_copy.insert(expn_id); | |
416331ca | 341 | } |
8bb4bdeb XL |
342 | } |
343 | ||
0731742a | 344 | impl<'a> Resolver<'a> { |
416331ca XL |
345 | /// Resolve macro path with error reporting and recovery. |
346 | fn smart_resolve_macro_path( | |
b7449926 XL |
347 | &mut self, |
348 | path: &ast::Path, | |
349 | kind: MacroKind, | |
350 | parent_scope: &ParentScope<'a>, | |
351 | force: bool, | |
416331ca | 352 | ) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> { |
dfeec247 XL |
353 | let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force) |
354 | { | |
416331ca XL |
355 | Ok((Some(ext), res)) => (ext, res), |
356 | // Use dummy syntax extensions for unresolved macros for better recovery. | |
357 | Ok((None, res)) => (self.dummy_ext(kind), res), | |
358 | Err(Determinacy::Determined) => (self.dummy_ext(kind), Res::Err), | |
359 | Err(Determinacy::Undetermined) => return Err(Indeterminate), | |
360 | }; | |
b7449926 XL |
361 | |
362 | // Report errors and enforce feature gates for the resolved macro. | |
416331ca XL |
363 | let features = self.session.features_untracked(); |
364 | for segment in &path.segments { | |
365 | if let Some(args) = &segment.args { | |
366 | self.session.span_err(args.span(), "generic arguments in macro path"); | |
367 | } | |
dfeec247 XL |
368 | if kind == MacroKind::Attr |
369 | && !features.rustc_attrs | |
370 | && segment.ident.as_str().starts_with("rustc") | |
371 | { | |
416331ca XL |
372 | let msg = |
373 | "attributes starting with `rustc` are reserved for use by the `rustc` compiler"; | |
60c5eb7d XL |
374 | feature_err(&self.session.parse_sess, sym::rustc_attrs, segment.ident.span, msg) |
375 | .emit(); | |
8bb4bdeb XL |
376 | } |
377 | } | |
378 | ||
48663c56 | 379 | match res { |
416331ca XL |
380 | Res::Def(DefKind::Macro(_), def_id) => { |
381 | if let Some(node_id) = self.definitions.as_local_node_id(def_id) { | |
382 | self.unused_macros.remove(&node_id); | |
383 | if self.proc_macro_stubs.contains(&node_id) { | |
384 | self.session.span_err( | |
385 | path.span, | |
386 | "can't use a procedural macro from the same crate that defines it", | |
387 | ); | |
b7449926 | 388 | } |
b7449926 | 389 | } |
83c7162d | 390 | } |
416331ca | 391 | Res::NonMacroAttr(..) | Res::Err => {} |
48663c56 | 392 | _ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"), |
416331ca | 393 | }; |
9fa01778 | 394 | |
416331ca | 395 | self.check_stability_and_deprecation(&ext, path); |
9fa01778 | 396 | |
416331ca | 397 | Ok(if ext.macro_kind() != kind { |
e1599b0c | 398 | let expected = kind.descr_expected(); |
e74abb32 XL |
399 | let path_str = pprust::path_to_string(path); |
400 | let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str); | |
dfeec247 XL |
401 | self.session |
402 | .struct_span_err(path.span, &msg) | |
403 | .span_label(path.span, format!("not {} {}", kind.article(), expected)) | |
404 | .emit(); | |
416331ca XL |
405 | // Use dummy syntax extensions for unexpected macro kinds for better recovery. |
406 | (self.dummy_ext(kind), Res::Err) | |
407 | } else { | |
408 | (ext, res) | |
409 | }) | |
9fa01778 XL |
410 | } |
411 | ||
416331ca | 412 | pub fn resolve_macro_path( |
b7449926 XL |
413 | &mut self, |
414 | path: &ast::Path, | |
416331ca | 415 | kind: Option<MacroKind>, |
b7449926 | 416 | parent_scope: &ParentScope<'a>, |
13cf67c4 | 417 | trace: bool, |
b7449926 | 418 | force: bool, |
416331ca | 419 | ) -> Result<(Option<Lrc<SyntaxExtension>>, Res), Determinacy> { |
0bf4aa26 | 420 | let path_span = path.span; |
13cf67c4 | 421 | let mut path = Segment::from_path(path); |
476ff2be | 422 | |
8faf50e0 | 423 | // Possibly apply the macro helper hack |
dfeec247 XL |
424 | if kind == Some(MacroKind::Bang) |
425 | && path.len() == 1 | |
426 | && path[0].ident.span.ctxt().outer_expn_data().local_inner_macros | |
427 | { | |
dc9dc135 | 428 | let root = Ident::new(kw::DollarCrate, path[0].ident.span); |
13cf67c4 | 429 | path.insert(0, Segment::from_ident(root)); |
8faf50e0 XL |
430 | } |
431 | ||
416331ca | 432 | let res = if path.len() > 1 { |
dfeec247 XL |
433 | let res = match self.resolve_path( |
434 | &path, | |
435 | Some(MacroNS), | |
436 | parent_scope, | |
437 | false, | |
438 | path_span, | |
439 | CrateLint::No, | |
440 | ) { | |
13cf67c4 | 441 | PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { |
48663c56 | 442 | Ok(path_res.base_res()) |
13cf67c4 | 443 | } |
476ff2be | 444 | PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined), |
532ac7d7 XL |
445 | PathResult::NonModule(..) |
446 | | PathResult::Indeterminate | |
447 | | PathResult::Failed { .. } => Err(Determinacy::Determined), | |
13cf67c4 | 448 | PathResult::Module(..) => unreachable!(), |
476ff2be | 449 | }; |
0bf4aa26 | 450 | |
13cf67c4 | 451 | if trace { |
416331ca | 452 | let kind = kind.expect("macro kind must be specified if tracing is enabled"); |
dfeec247 XL |
453 | self.multi_segment_macro_resolutions.push(( |
454 | path, | |
455 | path_span, | |
456 | kind, | |
457 | *parent_scope, | |
458 | res.ok(), | |
459 | )); | |
13cf67c4 | 460 | } |
476ff2be | 461 | |
48663c56 XL |
462 | self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span); |
463 | res | |
cc61c64b | 464 | } else { |
416331ca | 465 | let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro); |
0bf4aa26 | 466 | let binding = self.early_resolve_ident_in_lexical_scope( |
dfeec247 XL |
467 | path[0].ident, |
468 | scope_set, | |
469 | parent_scope, | |
470 | false, | |
471 | force, | |
472 | path_span, | |
0bf4aa26 | 473 | ); |
0731742a XL |
474 | if let Err(Determinacy::Undetermined) = binding { |
475 | return Err(Determinacy::Undetermined); | |
cc61c64b | 476 | } |
476ff2be | 477 | |
13cf67c4 | 478 | if trace { |
416331ca | 479 | let kind = kind.expect("macro kind must be specified if tracing is enabled"); |
dfeec247 XL |
480 | self.single_segment_macro_resolutions.push(( |
481 | path[0].ident, | |
482 | kind, | |
483 | *parent_scope, | |
484 | binding.ok(), | |
485 | )); | |
13cf67c4 | 486 | } |
8bb4bdeb | 487 | |
48663c56 XL |
488 | let res = binding.map(|binding| binding.res()); |
489 | self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span); | |
490 | res | |
416331ca XL |
491 | }; |
492 | ||
493 | res.map(|res| (self.get_macro(res), res)) | |
c30ab7b3 | 494 | } |
9e0c209e | 495 | |
0bf4aa26 | 496 | // Resolve an identifier in lexical scope. |
b7449926 XL |
497 | // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during |
498 | // expansion and import resolution (perhaps they can be merged in the future). | |
0731742a | 499 | // The function is used for resolving initial segments of macro paths (e.g., `foo` in |
13cf67c4 | 500 | // `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition. |
0bf4aa26 | 501 | crate fn early_resolve_ident_in_lexical_scope( |
b7449926 | 502 | &mut self, |
13cf67c4 XL |
503 | orig_ident: Ident, |
504 | scope_set: ScopeSet, | |
b7449926 XL |
505 | parent_scope: &ParentScope<'a>, |
506 | record_used: bool, | |
507 | force: bool, | |
508 | path_span: Span, | |
0bf4aa26 | 509 | ) -> Result<&'a NameBinding<'a>, Determinacy> { |
9fa01778 | 510 | bitflags::bitflags! { |
0bf4aa26 | 511 | struct Flags: u8 { |
60c5eb7d XL |
512 | const MACRO_RULES = 1 << 0; |
513 | const MODULE = 1 << 1; | |
514 | const DERIVE_HELPER_COMPAT = 1 << 2; | |
515 | const MISC_SUGGEST_CRATE = 1 << 3; | |
516 | const MISC_SUGGEST_SELF = 1 << 4; | |
517 | const MISC_FROM_PRELUDE = 1 << 5; | |
0bf4aa26 XL |
518 | } |
519 | } | |
b7449926 | 520 | |
b7449926 | 521 | assert!(force || !record_used); // `record_used` implies `force` |
13cf67c4 XL |
522 | |
523 | // Make sure `self`, `super` etc produce an error when passed to here. | |
416331ca | 524 | if orig_ident.is_path_segment_keyword() { |
13cf67c4 XL |
525 | return Err(Determinacy::Determined); |
526 | } | |
b7449926 | 527 | |
416331ca XL |
528 | let (ns, macro_kind, is_import) = match scope_set { |
529 | ScopeSet::All(ns, is_import) => (ns, None, is_import), | |
530 | ScopeSet::AbsolutePath(ns) => (ns, None, false), | |
531 | ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false), | |
532 | }; | |
533 | ||
b7449926 XL |
534 | // This is *the* result, resolution from the scope closest to the resolved identifier. |
535 | // However, sometimes this result is "weak" because it comes from a glob import or | |
536 | // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g. | |
537 | // mod m { ... } // solution in outer scope | |
538 | // { | |
539 | // use prefix::*; // imports another `m` - innermost solution | |
540 | // // weak, cannot shadow the outer `m`, need to report ambiguity error | |
541 | // m::mac!(); | |
542 | // } | |
543 | // So we have to save the innermost solution and continue searching in outer scopes | |
544 | // to detect potential ambiguities. | |
9fa01778 | 545 | let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None; |
416331ca | 546 | let mut determinacy = Determinacy::Determined; |
b7449926 XL |
547 | |
548 | // Go through all the scopes and try to resolve the name. | |
dfeec247 XL |
549 | let break_result = self.visit_scopes( |
550 | scope_set, | |
551 | parent_scope, | |
552 | orig_ident, | |
553 | |this, scope, use_prelude, ident| { | |
554 | let ok = |res, span, arenas| { | |
555 | Ok(( | |
556 | (res, ty::Visibility::Public, span, ExpnId::root()).to_name_binding(arenas), | |
557 | Flags::empty(), | |
558 | )) | |
559 | }; | |
560 | let result = match scope { | |
561 | Scope::DeriveHelpers(expn_id) => { | |
562 | if let Some(attr) = this | |
563 | .helper_attrs | |
564 | .get(&expn_id) | |
565 | .and_then(|attrs| attrs.iter().rfind(|i| ident == **i)) | |
566 | { | |
567 | let binding = ( | |
568 | Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper), | |
569 | ty::Visibility::Public, | |
570 | attr.span, | |
571 | expn_id, | |
572 | ) | |
573 | .to_name_binding(this.arenas); | |
574 | Ok((binding, Flags::empty())) | |
575 | } else { | |
576 | Err(Determinacy::Determined) | |
577 | } | |
60c5eb7d | 578 | } |
dfeec247 XL |
579 | Scope::DeriveHelpersCompat => { |
580 | let mut result = Err(Determinacy::Determined); | |
581 | for derive in parent_scope.derives { | |
582 | let parent_scope = &ParentScope { derives: &[], ..*parent_scope }; | |
583 | match this.resolve_macro_path( | |
584 | derive, | |
585 | Some(MacroKind::Derive), | |
586 | parent_scope, | |
587 | true, | |
588 | force, | |
589 | ) { | |
590 | Ok((Some(ext), _)) => { | |
591 | if ext.helper_attrs.contains(&ident.name) { | |
592 | let binding = ( | |
593 | Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper), | |
594 | ty::Visibility::Public, | |
595 | derive.span, | |
596 | ExpnId::root(), | |
597 | ) | |
598 | .to_name_binding(this.arenas); | |
599 | result = Ok((binding, Flags::DERIVE_HELPER_COMPAT)); | |
600 | break; | |
601 | } | |
602 | } | |
603 | Ok(_) | Err(Determinacy::Determined) => {} | |
604 | Err(Determinacy::Undetermined) => { | |
605 | result = Err(Determinacy::Undetermined) | |
606 | } | |
0bf4aa26 XL |
607 | } |
608 | } | |
dfeec247 | 609 | result |
0bf4aa26 | 610 | } |
dfeec247 XL |
611 | Scope::MacroRules(legacy_scope) => match legacy_scope { |
612 | LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident => { | |
613 | Ok((legacy_binding.binding, Flags::MACRO_RULES)) | |
13cf67c4 | 614 | } |
dfeec247 XL |
615 | LegacyScope::Invocation(invoc_id) |
616 | if !this.output_legacy_scopes.contains_key(&invoc_id) => | |
617 | { | |
618 | Err(Determinacy::Undetermined) | |
619 | } | |
620 | _ => Err(Determinacy::Determined), | |
621 | }, | |
622 | Scope::CrateRoot => { | |
623 | let root_ident = Ident::new(kw::PathRoot, ident.span); | |
624 | let root_module = this.resolve_crate_root(root_ident); | |
625 | let binding = this.resolve_ident_in_module_ext( | |
626 | ModuleOrUniformRoot::Module(root_module), | |
416331ca XL |
627 | ident, |
628 | ns, | |
629 | parent_scope, | |
dfeec247 | 630 | record_used, |
416331ca | 631 | path_span, |
dfeec247 XL |
632 | ); |
633 | match binding { | |
634 | Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)), | |
635 | Err((Determinacy::Undetermined, Weak::No)) => { | |
636 | return Some(Err(Determinacy::determined(force))); | |
637 | } | |
638 | Err((Determinacy::Undetermined, Weak::Yes)) => { | |
639 | Err(Determinacy::Undetermined) | |
b7449926 | 640 | } |
dfeec247 | 641 | Err((Determinacy::Determined, _)) => Err(Determinacy::Determined), |
b7449926 XL |
642 | } |
643 | } | |
dfeec247 XL |
644 | Scope::Module(module) => { |
645 | let adjusted_parent_scope = &ParentScope { module, ..*parent_scope }; | |
646 | let binding = this.resolve_ident_in_module_unadjusted_ext( | |
647 | ModuleOrUniformRoot::Module(module), | |
648 | ident, | |
649 | ns, | |
650 | adjusted_parent_scope, | |
651 | true, | |
652 | record_used, | |
653 | path_span, | |
654 | ); | |
655 | match binding { | |
656 | Ok(binding) => { | |
657 | let misc_flags = if ptr::eq(module, this.graph_root) { | |
658 | Flags::MISC_SUGGEST_CRATE | |
659 | } else if module.is_normal() { | |
660 | Flags::MISC_SUGGEST_SELF | |
661 | } else { | |
662 | Flags::empty() | |
663 | }; | |
664 | Ok((binding, Flags::MODULE | misc_flags)) | |
665 | } | |
666 | Err((Determinacy::Undetermined, Weak::No)) => { | |
667 | return Some(Err(Determinacy::determined(force))); | |
668 | } | |
669 | Err((Determinacy::Undetermined, Weak::Yes)) => { | |
670 | Err(Determinacy::Undetermined) | |
671 | } | |
672 | Err((Determinacy::Determined, _)) => Err(Determinacy::Determined), | |
673 | } | |
674 | } | |
675 | Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() { | |
676 | Some(ident) => ok( | |
677 | Res::NonMacroAttr(NonMacroAttrKind::Registered), | |
678 | ident.span, | |
679 | this.arenas, | |
680 | ), | |
681 | None => Err(Determinacy::Determined), | |
682 | }, | |
683 | Scope::MacroUsePrelude => { | |
684 | match this.macro_use_prelude.get(&ident.name).cloned() { | |
685 | Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)), | |
686 | None => Err(Determinacy::determined( | |
687 | this.graph_root.unexpanded_invocations.borrow().is_empty(), | |
688 | )), | |
689 | } | |
690 | } | |
691 | Scope::BuiltinAttrs => { | |
692 | if is_builtin_attr_name(ident.name) { | |
693 | ok(Res::NonMacroAttr(NonMacroAttrKind::Builtin), DUMMY_SP, this.arenas) | |
694 | } else { | |
695 | Err(Determinacy::Determined) | |
696 | } | |
7cac9316 | 697 | } |
dfeec247 XL |
698 | Scope::ExternPrelude => match this.extern_prelude_get(ident, !record_used) { |
699 | Some(binding) => Ok((binding, Flags::empty())), | |
700 | None => Err(Determinacy::determined( | |
701 | this.graph_root.unexpanded_invocations.borrow().is_empty(), | |
702 | )), | |
703 | }, | |
704 | Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() { | |
705 | Some(ident) => ok(Res::ToolMod, ident.span, this.arenas), | |
706 | None => Err(Determinacy::Determined), | |
707 | }, | |
708 | Scope::StdLibPrelude => { | |
709 | let mut result = Err(Determinacy::Determined); | |
710 | if let Some(prelude) = this.prelude { | |
711 | if let Ok(binding) = this.resolve_ident_in_module_unadjusted( | |
712 | ModuleOrUniformRoot::Module(prelude), | |
713 | ident, | |
714 | ns, | |
715 | parent_scope, | |
716 | false, | |
717 | path_span, | |
718 | ) { | |
719 | if use_prelude || this.is_builtin_macro(binding.res()) { | |
720 | result = Ok((binding, Flags::MISC_FROM_PRELUDE)); | |
721 | } | |
722 | } | |
723 | } | |
724 | result | |
725 | } | |
726 | Scope::BuiltinTypes => { | |
727 | match this.primitive_type_table.primitive_types.get(&ident.name).cloned() { | |
728 | Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas), | |
729 | None => Err(Determinacy::Determined), | |
730 | } | |
731 | } | |
732 | }; | |
733 | ||
734 | match result { | |
735 | Ok((binding, flags)) | |
736 | if sub_namespace_match(binding.macro_kind(), macro_kind) => | |
737 | { | |
738 | if !record_used { | |
739 | return Some(Ok(binding)); | |
740 | } | |
741 | ||
742 | if let Some((innermost_binding, innermost_flags)) = innermost_result { | |
743 | // Found another solution, if the first one was "weak", report an error. | |
744 | let (res, innermost_res) = (binding.res(), innermost_binding.res()); | |
745 | if res != innermost_res { | |
746 | let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin); | |
747 | let is_derive_helper_compat = |res, flags: Flags| { | |
748 | res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper) | |
749 | && flags.contains(Flags::DERIVE_HELPER_COMPAT) | |
750 | }; | |
b7449926 | 751 | |
dfeec247 XL |
752 | let ambiguity_error_kind = if is_import { |
753 | Some(AmbiguityKind::Import) | |
754 | } else if innermost_res == builtin || res == builtin { | |
755 | Some(AmbiguityKind::BuiltinAttr) | |
756 | } else if is_derive_helper_compat(innermost_res, innermost_flags) | |
757 | || is_derive_helper_compat(res, flags) | |
758 | { | |
759 | Some(AmbiguityKind::DeriveHelper) | |
760 | } else if innermost_flags.contains(Flags::MACRO_RULES) | |
761 | && flags.contains(Flags::MODULE) | |
762 | && !this | |
763 | .disambiguate_legacy_vs_modern(innermost_binding, binding) | |
764 | || flags.contains(Flags::MACRO_RULES) | |
765 | && innermost_flags.contains(Flags::MODULE) | |
766 | && !this.disambiguate_legacy_vs_modern( | |
767 | binding, | |
768 | innermost_binding, | |
769 | ) | |
770 | { | |
771 | Some(AmbiguityKind::LegacyVsModern) | |
772 | } else if innermost_binding.is_glob_import() { | |
773 | Some(AmbiguityKind::GlobVsOuter) | |
774 | } else if innermost_binding | |
775 | .may_appear_after(parent_scope.expansion, binding) | |
776 | { | |
777 | Some(AmbiguityKind::MoreExpandedVsOuter) | |
13cf67c4 | 778 | } else { |
dfeec247 | 779 | None |
13cf67c4 | 780 | }; |
dfeec247 XL |
781 | if let Some(kind) = ambiguity_error_kind { |
782 | let misc = |f: Flags| { | |
783 | if f.contains(Flags::MISC_SUGGEST_CRATE) { | |
784 | AmbiguityErrorMisc::SuggestCrate | |
785 | } else if f.contains(Flags::MISC_SUGGEST_SELF) { | |
786 | AmbiguityErrorMisc::SuggestSelf | |
787 | } else if f.contains(Flags::MISC_FROM_PRELUDE) { | |
788 | AmbiguityErrorMisc::FromPrelude | |
789 | } else { | |
790 | AmbiguityErrorMisc::None | |
791 | } | |
792 | }; | |
793 | this.ambiguity_errors.push(AmbiguityError { | |
794 | kind, | |
795 | ident: orig_ident, | |
796 | b1: innermost_binding, | |
797 | b2: binding, | |
798 | misc1: misc(innermost_flags), | |
799 | misc2: misc(flags), | |
800 | }); | |
801 | return Some(Ok(innermost_binding)); | |
802 | } | |
13cf67c4 | 803 | } |
dfeec247 XL |
804 | } else { |
805 | // Found the first solution. | |
806 | innermost_result = Some((binding, flags)); | |
476ff2be | 807 | } |
476ff2be | 808 | } |
dfeec247 XL |
809 | Ok(..) | Err(Determinacy::Determined) => {} |
810 | Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined, | |
b7449926 | 811 | } |
13cf67c4 | 812 | |
dfeec247 XL |
813 | None |
814 | }, | |
815 | ); | |
13cf67c4 | 816 | |
416331ca XL |
817 | if let Some(break_result) = break_result { |
818 | return break_result; | |
b7449926 | 819 | } |
476ff2be | 820 | |
b7449926 | 821 | // The first found solution was the only one, return it. |
532ac7d7 | 822 | if let Some((binding, _)) = innermost_result { |
0bf4aa26 | 823 | return Ok(binding); |
b7449926 XL |
824 | } |
825 | ||
60c5eb7d | 826 | Err(Determinacy::determined(determinacy == Determinacy::Determined || force)) |
476ff2be SL |
827 | } |
828 | ||
e1599b0c | 829 | crate fn finalize_macro_resolutions(&mut self) { |
dfeec247 XL |
830 | let check_consistency = |this: &mut Self, |
831 | path: &[Segment], | |
832 | span, | |
833 | kind: MacroKind, | |
834 | initial_res: Option<Res>, | |
835 | res: Res| { | |
48663c56 XL |
836 | if let Some(initial_res) = initial_res { |
837 | if res != initial_res && res != Res::Err && this.ambiguity_errors.is_empty() { | |
13cf67c4 XL |
838 | // Make sure compilation does not succeed if preferred macro resolution |
839 | // has changed after the macro had been expanded. In theory all such | |
840 | // situations should be reported as ambiguity errors, so this is a bug. | |
60c5eb7d | 841 | span_bug!(span, "inconsistent resolution for a macro"); |
13cf67c4 XL |
842 | } |
843 | } else { | |
844 | // It's possible that the macro was unresolved (indeterminate) and silently | |
845 | // expanded into a dummy fragment for recovery during expansion. | |
846 | // Now, post-expansion, the resolution may succeed, but we can't change the | |
847 | // past and need to report an error. | |
848 | // However, non-speculative `resolve_path` can successfully return private items | |
849 | // even if speculative `resolve_path` returned nothing previously, so we skip this | |
850 | // less informative error if the privacy error is reported elsewhere. | |
851 | if this.privacy_errors.is_empty() { | |
dfeec247 XL |
852 | let msg = format!( |
853 | "cannot determine resolution for the {} `{}`", | |
854 | kind.descr(), | |
855 | Segment::names_to_string(path) | |
856 | ); | |
13cf67c4 XL |
857 | let msg_note = "import resolution is stuck, try simplifying macro imports"; |
858 | this.session.struct_span_err(span, &msg).note(msg_note).emit(); | |
859 | } | |
860 | } | |
861 | }; | |
862 | ||
e1599b0c | 863 | let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions); |
48663c56 | 864 | for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions { |
13cf67c4 | 865 | // FIXME: Path resolution will ICE if segment IDs present. |
dfeec247 XL |
866 | for seg in &mut path { |
867 | seg.id = None; | |
868 | } | |
416331ca | 869 | match self.resolve_path( |
dfeec247 XL |
870 | &path, |
871 | Some(MacroNS), | |
872 | &parent_scope, | |
873 | true, | |
874 | path_span, | |
875 | CrateLint::No, | |
416331ca | 876 | ) { |
13cf67c4 | 877 | PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { |
48663c56 XL |
878 | let res = path_res.base_res(); |
879 | check_consistency(self, &path, path_span, kind, initial_res, res); | |
13cf67c4 | 880 | } |
532ac7d7 XL |
881 | path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => { |
882 | let (span, label) = if let PathResult::Failed { span, label, .. } = path_res { | |
883 | (span, label) | |
13cf67c4 | 884 | } else { |
dfeec247 XL |
885 | ( |
886 | path_span, | |
887 | format!( | |
888 | "partially resolved path in {} {}", | |
889 | kind.article(), | |
890 | kind.descr() | |
891 | ), | |
892 | ) | |
13cf67c4 | 893 | }; |
dfeec247 XL |
894 | self.report_error( |
895 | span, | |
896 | ResolutionError::FailedToResolve { label, suggestion: None }, | |
897 | ); | |
476ff2be | 898 | } |
13cf67c4 | 899 | PathResult::Module(..) | PathResult::Indeterminate => unreachable!(), |
476ff2be SL |
900 | } |
901 | } | |
902 | ||
e1599b0c | 903 | let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions); |
13cf67c4 | 904 | for (ident, kind, parent_scope, initial_binding) in macro_resolutions { |
dfeec247 XL |
905 | match self.early_resolve_ident_in_lexical_scope( |
906 | ident, | |
907 | ScopeSet::Macro(kind), | |
908 | &parent_scope, | |
909 | true, | |
910 | true, | |
911 | ident.span, | |
912 | ) { | |
0bf4aa26 | 913 | Ok(binding) => { |
48663c56 | 914 | let initial_res = initial_binding.map(|initial_binding| { |
13cf67c4 | 915 | self.record_use(ident, MacroNS, initial_binding, false); |
48663c56 | 916 | initial_binding.res() |
13cf67c4 | 917 | }); |
48663c56 | 918 | let res = binding.res(); |
13cf67c4 | 919 | let seg = Segment::from_ident(ident); |
48663c56 | 920 | check_consistency(self, &[seg], ident.span, kind, initial_res, res); |
94b46f34 | 921 | } |
0bf4aa26 | 922 | Err(..) => { |
e1599b0c XL |
923 | let expected = kind.descr_expected(); |
924 | let msg = format!("cannot find {} `{}` in this scope", expected, ident); | |
0bf4aa26 | 925 | let mut err = self.session.struct_span_err(ident.span, &msg); |
416331ca | 926 | self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident); |
8bb4bdeb | 927 | err.emit(); |
b7449926 | 928 | } |
0bf4aa26 | 929 | } |
c30ab7b3 | 930 | } |
b7449926 | 931 | |
e1599b0c | 932 | let builtin_attrs = mem::take(&mut self.builtin_attrs); |
b7449926 | 933 | for (ident, parent_scope) in builtin_attrs { |
13cf67c4 | 934 | let _ = self.early_resolve_ident_in_lexical_scope( |
dfeec247 XL |
935 | ident, |
936 | ScopeSet::Macro(MacroKind::Attr), | |
937 | &parent_scope, | |
938 | true, | |
939 | true, | |
940 | ident.span, | |
0bf4aa26 | 941 | ); |
b7449926 | 942 | } |
9e0c209e | 943 | } |
9e0c209e | 944 | |
e74abb32 | 945 | fn check_stability_and_deprecation(&mut self, ext: &SyntaxExtension, path: &ast::Path) { |
416331ca XL |
946 | let span = path.span; |
947 | if let Some(stability) = &ext.stability { | |
948 | if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level { | |
949 | let feature = stability.feature; | |
950 | if !self.active_features.contains(&feature) && !span.allows_unstable(feature) { | |
e74abb32 XL |
951 | let node_id = ast::CRATE_NODE_ID; |
952 | let lint_buffer = &mut self.lint_buffer; | |
dfeec247 XL |
953 | let soft_handler = |
954 | |lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg); | |
e74abb32 | 955 | stability::report_unstable( |
dfeec247 XL |
956 | self.session, |
957 | feature, | |
958 | reason, | |
959 | issue, | |
960 | is_soft, | |
961 | span, | |
962 | soft_handler, | |
e74abb32 | 963 | ); |
416331ca XL |
964 | } |
965 | } | |
966 | if let Some(depr) = &stability.rustc_depr { | |
e74abb32 XL |
967 | let path = pprust::path_to_string(path); |
968 | let (message, lint) = stability::rustc_deprecation_message(depr, &path); | |
416331ca | 969 | stability::early_report_deprecation( |
dfeec247 XL |
970 | &mut self.lint_buffer, |
971 | &message, | |
972 | depr.suggestion, | |
973 | lint, | |
974 | span, | |
416331ca XL |
975 | ); |
976 | } | |
977 | } | |
978 | if let Some(depr) = &ext.deprecation { | |
e74abb32 XL |
979 | let path = pprust::path_to_string(&path); |
980 | let (message, lint) = stability::deprecation_message(depr, &path); | |
981 | stability::early_report_deprecation(&mut self.lint_buffer, &message, None, lint, span); | |
416331ca XL |
982 | } |
983 | } | |
984 | ||
dfeec247 XL |
985 | fn prohibit_imported_non_macro_attrs( |
986 | &self, | |
987 | binding: Option<&'a NameBinding<'a>>, | |
988 | res: Option<Res>, | |
989 | span: Span, | |
990 | ) { | |
48663c56 | 991 | if let Some(Res::NonMacroAttr(kind)) = res { |
69743fb6 | 992 | if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) { |
60c5eb7d XL |
993 | let msg = |
994 | format!("cannot use {} {} through an import", kind.article(), kind.descr()); | |
69743fb6 XL |
995 | let mut err = self.session.struct_span_err(span, &msg); |
996 | if let Some(binding) = binding { | |
997 | err.span_note(binding.span, &format!("the {} imported here", kind.descr())); | |
998 | } | |
999 | err.emit(); | |
1000 | } | |
1001 | } | |
1002 | } | |
1003 | ||
416331ca XL |
1004 | crate fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) { |
1005 | // Reserve some names that are not quite covered by the general check | |
1006 | // performed on `Resolver::builtin_attrs`. | |
1007 | if ident.name == sym::cfg || ident.name == sym::cfg_attr || ident.name == sym::derive { | |
1008 | let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind()); | |
1009 | if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) { | |
1010 | self.session.span_err( | |
dfeec247 XL |
1011 | ident.span, |
1012 | &format!("name `{}` is reserved in attribute namespace", ident), | |
416331ca XL |
1013 | ); |
1014 | } | |
dc9dc135 | 1015 | } |
416331ca | 1016 | } |
dc9dc135 | 1017 | |
60c5eb7d XL |
1018 | /// Compile the macro into a `SyntaxExtension` and possibly replace |
1019 | /// its expander to a pre-defined one for built-in macros. | |
e1599b0c | 1020 | crate fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> SyntaxExtension { |
e74abb32 | 1021 | let mut result = compile_declarative_macro( |
dfeec247 XL |
1022 | &self.session.parse_sess, |
1023 | self.session.features_untracked(), | |
1024 | item, | |
1025 | edition, | |
416331ca | 1026 | ); |
8bb4bdeb | 1027 | |
416331ca XL |
1028 | if result.is_builtin { |
1029 | // The macro was marked with `#[rustc_builtin_macro]`. | |
1030 | if let Some(ext) = self.builtin_macros.remove(&item.ident.name) { | |
60c5eb7d XL |
1031 | // The macro is a built-in, replace its expander function |
1032 | // while still taking everything else from the source code. | |
1033 | result.kind = ext.kind; | |
9e0c209e | 1034 | } else { |
416331ca XL |
1035 | let msg = format!("cannot find a built-in macro with name `{}`", item.ident); |
1036 | self.session.span_err(item.span, &msg); | |
9e0c209e SL |
1037 | } |
1038 | } | |
32a655c1 | 1039 | |
e1599b0c | 1040 | result |
32a655c1 | 1041 | } |
9e0c209e | 1042 | } |