macros.rs 47.9 KB
Newer Older
1 2 3
//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.

4
use crate::imports::ImportResolver;
M
Mark Rousskov 已提交
5 6 7 8
use crate::Namespace::*;
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy};
use crate::{CrateLint, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak};
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding};
U
Ujjwal Sharma 已提交
9
use rustc_ast::{self as ast, NodeId};
10
use rustc_ast_lowering::ResolverAstLowering;
11
use rustc_ast_pretty::pprust;
12
use rustc_attr::StabilityLevel;
13
use rustc_data_structures::fx::FxHashSet;
14
use rustc_expand::base::{Indeterminate, InvocationRes, ResolverExpand, SyntaxExtension};
15 16
use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{AstFragment, AstFragmentKind, Invocation, InvocationKind};
17
use rustc_feature::is_builtin_attr_name;
18 19
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
use rustc_hir::def_id;
20 21
use rustc_middle::middle::stability;
use rustc_middle::{span_bug, ty};
22 23
use rustc_session::lint::builtin::UNUSED_MACROS;
use rustc_session::Session;
24
use rustc_span::edition::Edition;
25
use rustc_span::hygiene::{self, ExpnData, ExpnId, ExpnKind};
26
use rustc_span::symbol::{kw, sym, Ident, Symbol};
27
use rustc_span::{Span, DUMMY_SP};
28

29
use rustc_data_structures::sync::Lrc;
30
use rustc_span::hygiene::{AstPass, MacroKind};
M
Mark Rousskov 已提交
31
use std::{mem, ptr};
32

33
type Res = def::Res<NodeId>;
L
ljedrz 已提交
34

35
/// Binding produced by a `macro_rules` item.
36
/// Not modularized, can shadow previous `macro_rules` bindings, etc.
37
#[derive(Debug)]
38
pub struct MacroRulesBinding<'a> {
39
    crate binding: &'a NameBinding<'a>,
40 41
    /// `macro_rules` scope into which the `macro_rules` item was planted.
    crate parent_macro_rules_scope: MacroRulesScope<'a>,
42
    crate ident: Ident,
43 44
}

A
Alexander Regueiro 已提交
45 46 47
/// The scope introduced by a `macro_rules!` macro.
/// This starts at the macro's definition and ends at the end of the macro's parent
/// module (named or unnamed), or even further if it escapes with `#[macro_use]`.
48
/// Some macro invocations need to introduce `macro_rules` scopes too because they
A
Alexander Regueiro 已提交
49
/// can potentially expand into macro definitions.
50
#[derive(Copy, Clone, Debug)]
51
pub enum MacroRulesScope<'a> {
52
    /// Empty "root" scope at the crate start containing no names.
J
Jeffrey Seyfried 已提交
53
    Empty,
A
Alexander Regueiro 已提交
54
    /// The scope introduced by a `macro_rules!` macro definition.
55
    Binding(&'a MacroRulesBinding<'a>),
A
Alexander Regueiro 已提交
56
    /// The scope introduced by a macro invocation that can potentially
57
    /// create a `macro_rules!` macro definition.
58
    Invocation(ExpnId),
59 60
}

61 62 63
// Macro namespace is separated into two sub-namespaces, one for bang macros and
// one for attribute-like macros (attributes, derives).
// We ignore resolutions from one sub-namespace when searching names in scope for another.
64
fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
65
    #[derive(PartialEq)]
M
Mark Rousskov 已提交
66 67 68 69
    enum SubNS {
        Bang,
        AttrLike,
    }
70
    let sub_ns = |kind| match kind {
71 72
        MacroKind::Bang => SubNS::Bang,
        MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
73
    };
74 75
    let candidate = candidate.map(sub_ns);
    let requirement = requirement.map(sub_ns);
76
    // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
77
    candidate.is_none() || requirement.is_none() || candidate == requirement
78 79
}

80 81 82
// We don't want to format a path using pretty-printing,
// `format!("{}", path)`, because that tries to insert
// line-breaks and is slow.
83 84
fn fast_print_path(path: &ast::Path) -> Symbol {
    if path.segments.len() == 1 {
85
        path.segments[0].ident.name
86 87 88 89 90 91 92 93 94
    } else {
        let mut path_str = String::with_capacity(64);
        for (i, segment) in path.segments.iter().enumerate() {
            if i != 0 {
                path_str.push_str("::");
            }
            if segment.ident.name != kw::PathRoot {
                path_str.push_str(&segment.ident.as_str())
            }
95
        }
96
        Symbol::intern(&path_str)
97 98 99
    }
}

V
Vadim Petrochenkov 已提交
100
/// The code common between processing `#![register_tool]` and `#![register_attr]`.
101 102 103 104 105 106 107
fn registered_idents(
    sess: &Session,
    attrs: &[ast::Attribute],
    attr_name: Symbol,
    descr: &str,
) -> FxHashSet<Ident> {
    let mut registered = FxHashSet::default();
108
    for attr in sess.filter_by_name(attrs, attr_name) {
109 110
        for nested_meta in attr.meta_item_list().unwrap_or_default() {
            match nested_meta.ident() {
M
Mark Rousskov 已提交
111 112 113 114 115 116 117
                Some(ident) => {
                    if let Some(old_ident) = registered.replace(ident) {
                        let msg = format!("{} `{}` was already registered", descr, ident);
                        sess.struct_span_err(ident.span, &msg)
                            .span_label(old_ident.span, "already registered here")
                            .emit();
                    }
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                }
                None => {
                    let msg = format!("`{}` only accepts identifiers", attr_name);
                    let span = nested_meta.span();
                    sess.struct_span_err(span, &msg).span_label(span, "not an identifier").emit();
                }
            }
        }
    }
    registered
}

crate fn registered_attrs_and_tools(
    sess: &Session,
    attrs: &[ast::Attribute],
) -> (FxHashSet<Ident>, FxHashSet<Ident>) {
    let registered_attrs = registered_idents(sess, attrs, sym::register_attr, "attribute");
    let mut registered_tools = registered_idents(sess, attrs, sym::register_tool, "tool");
    // We implicitly add `rustfmt` and `clippy` to known tools,
    // but it's not an error to register them explicitly.
    let predefined_tools = [sym::clippy, sym::rustfmt];
    registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
    (registered_attrs, registered_tools)
}

143
impl<'a> ResolverExpand for Resolver<'a> {
144
    fn next_node_id(&mut self) -> NodeId {
M
Mark Rousskov 已提交
145
        self.next_node_id()
146 147
    }

148 149 150 151 152 153
    fn resolve_dollar_crates(&mut self) {
        hygiene::update_dollar_crate_names(|ctxt| {
            let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
            match self.resolve_crate_root(ident).kind {
                ModuleKind::Def(.., name) if name != kw::Invalid => name,
                _ => kw::Crate,
154
            }
155
        });
156 157
    }

158
    fn visit_ast_fragment_with_placeholders(&mut self, expansion: ExpnId, fragment: &AstFragment) {
159
        // Integrate the new AST fragment into all the definition and module structures.
160 161
        // We are inside the `expansion` now, but other parent scope components are still the same.
        let parent_scope = ParentScope { expansion, ..self.invocation_parent_scopes[&expansion] };
162 163
        let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
        self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
164

165
        parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
166 167
    }

168
    fn register_builtin_macro(&mut self, ident: Ident, ext: SyntaxExtension) {
169
        if self.builtin_macros.insert(ident.name, ext).is_some() {
M
Mark Rousskov 已提交
170 171
            self.session
                .span_err(ident.span, &format!("built-in macro `{}` was already defined", ident));
172
        }
173 174
    }

M
Matthew Jasper 已提交
175 176
    // Create a new Expansion with a definition site of the provided module, or
    // 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 181 182
        pass: AstPass,
        features: &[Symbol],
        parent_module_id: Option<NodeId>,
183 184 185 186 187 188
    ) -> ExpnId {
        let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
            ExpnKind::AstPass(pass),
            call_site,
            self.session.edition(),
            features.into(),
A
Aaron Hill 已提交
189
            None,
190 191
        )));

192
        let parent_scope = if let Some(module_id) = parent_module_id {
193
            let parent_def_id = self.local_def_id(module_id);
194
            self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id.to_def_id());
195 196 197 198 199 200 201 202 203
            self.module_map[&parent_def_id]
        } else {
            self.definitions.add_parent_module_of_macro_def(
                expn_id,
                def_id::DefId::local(def_id::CRATE_DEF_INDEX),
            );
            self.empty_module
        };
        self.ast_transform_scopes.insert(expn_id, parent_scope);
204
        expn_id
205 206
    }

207
    fn resolve_imports(&mut self) {
208
        ImportResolver { r: self }.resolve_imports()
209 210
    }

211
    fn resolve_macro_invocation(
M
Mark Rousskov 已提交
212 213 214 215
        &mut self,
        invoc: &Invocation,
        eager_expansion_root: ExpnId,
        force: bool,
216
    ) -> Result<InvocationRes, Indeterminate> {
217 218 219 220 221 222 223
        let invoc_id = invoc.expansion_data.id;
        let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) {
            Some(parent_scope) => *parent_scope,
            None => {
                // If there's no entry in the table, then we are resolving an eagerly expanded
                // macro, which should inherit its parent scope from its eager expansion root -
                // the macro that requested this eager expansion.
M
Mark Rousskov 已提交
224 225 226
                let parent_scope = *self
                    .invocation_parent_scopes
                    .get(&eager_expansion_root)
227 228 229 230 231 232
                    .expect("non-eager expansion without a parent scope");
                self.invocation_parent_scopes.insert(invoc_id, parent_scope);
                parent_scope
            }
        };

233
        let (path, kind, derives, after_derive) = match invoc.kind {
M
Mark Rousskov 已提交
234 235 236 237 238 239 240 241
            InvocationKind::Attr { ref attr, ref derives, after_derive, .. } => (
                &attr.get_normal_item().path,
                MacroKind::Attr,
                self.arenas.alloc_ast_paths(derives),
                after_derive,
            ),
            InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang, &[][..], false),
            InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive, &[][..], false),
242
            InvocationKind::DeriveContainer { ref derives, .. } => {
243 244 245 246 247 248 249 250
                // Block expansion of the container until we resolve all derives in it.
                // This is required for two reasons:
                // - Derive helper attributes are in scope for the item to which the `#[derive]`
                //   is applied, so they have to be produced by the container's expansion rather
                //   than by individual derives.
                // - Derives in the container need to know whether one of them is a built-in `Copy`.
                // FIXME: Try to avoid repeated resolutions for derives here and in expansion.
                let mut exts = Vec::new();
251
                let mut helper_attrs = Vec::new();
252
                for path in derives {
M
Mark Rousskov 已提交
253 254 255 256 257 258 259 260 261
                    exts.push(
                        match self.resolve_macro_path(
                            path,
                            Some(MacroKind::Derive),
                            &parent_scope,
                            true,
                            force,
                        ) {
                            Ok((Some(ext), _)) => {
262 263 264 265 266 267 268
                                let span = path
                                    .segments
                                    .last()
                                    .unwrap()
                                    .ident
                                    .span
                                    .normalize_to_macros_2_0();
M
Mark Rousskov 已提交
269 270 271 272 273 274 275
                                helper_attrs.extend(
                                    ext.helper_attrs.iter().map(|name| Ident::new(*name, span)),
                                );
                                if ext.is_derive_copy {
                                    self.add_derive_copy(invoc_id);
                                }
                                ext
276
                            }
M
Mark Rousskov 已提交
277 278 279 280 281 282
                            Ok(_) | Err(Determinacy::Determined) => {
                                self.dummy_ext(MacroKind::Derive)
                            }
                            Err(Determinacy::Undetermined) => return Err(Indeterminate),
                        },
                    )
283
                }
284
                self.helper_attrs.insert(invoc_id, helper_attrs);
285
                return Ok(InvocationRes::DeriveContainer(exts));
286
            }
287
        };
288

289
        // Derives are not included when `invocations` are collected, so we have to add them here.
290
        let parent_scope = &ParentScope { derives, ..parent_scope };
291 292
        let node_id = self.lint_node_id(eager_expansion_root);
        let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, node_id, force)?;
293

294
        let span = invoc.span();
A
Aaron Hill 已提交
295 296 297 298 299 300
        invoc_id.set_expn_data(ext.expn_data(
            parent_scope.expansion,
            span,
            fast_print_path(path),
            res.opt_def_id(),
        ));
301

A
Aaron Hill 已提交
302
        if let Res::Def(_, _) = res {
303
            if after_derive {
304 305
                self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
            }
306 307
            let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
            self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
308
        }
309

C
Caio 已提交
310 311
        match invoc.fragment_kind {
            AstFragmentKind::Arms
M
Mark Rousskov 已提交
312 313 314 315 316 317
            | AstFragmentKind::Fields
            | AstFragmentKind::FieldPats
            | AstFragmentKind::GenericParams
            | AstFragmentKind::Params
            | AstFragmentKind::StructFields
            | AstFragmentKind::Variants => {
C
Caio 已提交
318 319 320
                if let Res::Def(..) = res {
                    self.session.span_err(
                        span,
M
Mark Rousskov 已提交
321 322 323 324 325
                        &format!(
                            "expected an inert attribute, found {} {}",
                            res.article(),
                            res.descr()
                        ),
C
Caio 已提交
326 327 328
                    );
                    return Ok(InvocationRes::Single(self.dummy_ext(kind)));
                }
M
Mark Rousskov 已提交
329
            }
C
Caio 已提交
330 331 332
            _ => {}
        }

333
        Ok(InvocationRes::Single(ext))
334 335
    }

336
    fn check_unused_macros(&mut self) {
337
        for (_, &(node_id, span)) in self.unused_macros.iter() {
338
            self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition");
E
est31 已提交
339
        }
340
    }
341

342
    fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId {
343 344 345
        self.invocation_parents
            .get(&expn_id)
            .map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[*id])
346 347
    }

348
    fn has_derive_copy(&self, expn_id: ExpnId) -> bool {
349
        self.containers_deriving_copy.contains(&expn_id)
350 351
    }

352
    fn add_derive_copy(&mut self, expn_id: ExpnId) {
353
        self.containers_deriving_copy.insert(expn_id);
354
    }
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

    // The function that implements the resolution logic of `#[cfg_accessible(path)]`.
    // Returns true if the path can certainly be resolved in one of three namespaces,
    // returns false if the path certainly cannot be resolved in any of the three namespaces.
    // Returns `Indeterminate` if we cannot give a certain answer yet.
    fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate> {
        let span = path.span;
        let path = &Segment::from_path(path);
        let parent_scope = self.invocation_parent_scopes[&expn_id];

        let mut indeterminate = false;
        for ns in [TypeNS, ValueNS, MacroNS].iter().copied() {
            match self.resolve_path(path, Some(ns), &parent_scope, false, span, CrateLint::No) {
                PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true),
                PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
                    return Ok(true);
                }
                PathResult::Indeterminate => indeterminate = true,
                // FIXME: `resolve_path` is not ready to report partially resolved paths
                // correctly, so we just report an error if the path was reported as unresolved.
                // This needs to be fixed for `cfg_accessible` to be useful.
                PathResult::NonModule(..) | PathResult::Failed { .. } => {}
                PathResult::Module(_) => panic!("unexpected path resolution"),
            }
        }

        if indeterminate {
            return Err(Indeterminate);
        }

        self.session
            .struct_span_err(span, "not sure whether the path is accessible or not")
            .span_note(span, "`cfg_accessible` is not fully implemented")
            .emit();
        Ok(false)
    }
391 392
}

J
John Kåre Alsaker 已提交
393
impl<'a> Resolver<'a> {
394 395
    /// Resolve macro path with error reporting and recovery.
    fn smart_resolve_macro_path(
396 397 398 399
        &mut self,
        path: &ast::Path,
        kind: MacroKind,
        parent_scope: &ParentScope<'a>,
400
        node_id: NodeId,
401
        force: bool,
402
    ) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
M
Mark Rousskov 已提交
403 404
        let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
        {
405 406 407 408 409 410
            Ok((Some(ext), res)) => (ext, res),
            // Use dummy syntax extensions for unresolved macros for better recovery.
            Ok((None, res)) => (self.dummy_ext(kind), res),
            Err(Determinacy::Determined) => (self.dummy_ext(kind), Res::Err),
            Err(Determinacy::Undetermined) => return Err(Indeterminate),
        };
411

412
        // Report errors for the resolved macro.
413 414 415
        for segment in &path.segments {
            if let Some(args) = &segment.args {
                self.session.span_err(args.span(), "generic arguments in macro path");
416
            }
417 418 419 420 421
            if kind == MacroKind::Attr && segment.ident.as_str().starts_with("rustc") {
                self.session.span_err(
                    segment.ident.span,
                    "attributes starting with `rustc` are reserved for use by the `rustc` compiler",
                );
422
            }
423
        }
424

425
        match res {
426
            Res::Def(DefKind::Macro(_), def_id) => {
427 428 429
                if let Some(def_id) = def_id.as_local() {
                    self.unused_macros.remove(&def_id);
                    if self.proc_macro_stubs.contains(&def_id) {
430 431 432 433 434
                        self.session.span_err(
                            path.span,
                            "can't use a procedural macro from the same crate that defines it",
                        );
                    }
435
                }
436
            }
437
            Res::NonMacroAttr(..) | Res::Err => {}
438
            _ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
439
        };
440

441
        self.check_stability_and_deprecation(&ext, path, node_id);
442

443
        Ok(if ext.macro_kind() != kind {
444
            let expected = kind.descr_expected();
445 446
            let path_str = pprust::path_to_string(path);
            let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
M
Mark Rousskov 已提交
447 448 449 450
            self.session
                .struct_span_err(path.span, &msg)
                .span_label(path.span, format!("not {} {}", kind.article(), expected))
                .emit();
451 452
            // Use dummy syntax extensions for unexpected macro kinds for better recovery.
            (self.dummy_ext(kind), Res::Err)
453
        } else {
454
            (ext, res)
455
        })
456
    }
457

458
    pub fn resolve_macro_path(
459 460
        &mut self,
        path: &ast::Path,
461
        kind: Option<MacroKind>,
462
        parent_scope: &ParentScope<'a>,
463
        trace: bool,
464
        force: bool,
465
    ) -> Result<(Option<Lrc<SyntaxExtension>>, Res), Determinacy> {
466
        let path_span = path.span;
N
Nick Cameron 已提交
467
        let mut path = Segment::from_path(path);
468

469
        // Possibly apply the macro helper hack
M
Mark Rousskov 已提交
470 471 472 473
        if kind == Some(MacroKind::Bang)
            && path.len() == 1
            && path[0].ident.span.ctxt().outer_expn_data().local_inner_macros
        {
474
            let root = Ident::new(kw::DollarCrate, path[0].ident.span);
N
Nick Cameron 已提交
475
            path.insert(0, Segment::from_ident(root));
476 477
        }

478
        let res = if path.len() > 1 {
M
Mark Rousskov 已提交
479 480 481 482 483 484 485 486
            let res = match self.resolve_path(
                &path,
                Some(MacroNS),
                parent_scope,
                false,
                path_span,
                CrateLint::No,
            ) {
487
                PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
488
                    Ok(path_res.base_res())
489
                }
490
                PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
491 492 493
                PathResult::NonModule(..)
                | PathResult::Indeterminate
                | PathResult::Failed { .. } => Err(Determinacy::Determined),
494
                PathResult::Module(..) => unreachable!(),
495
            };
496

497
            if trace {
498
                let kind = kind.expect("macro kind must be specified if tracing is enabled");
M
Mark Rousskov 已提交
499 500 501 502 503 504 505
                self.multi_segment_macro_resolutions.push((
                    path,
                    path_span,
                    kind,
                    *parent_scope,
                    res.ok(),
                ));
506
            }
507

508 509
            self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
            res
510
        } else {
511
            let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro);
512
            let binding = self.early_resolve_ident_in_lexical_scope(
M
Mark Rousskov 已提交
513 514 515 516 517 518
                path[0].ident,
                scope_set,
                parent_scope,
                false,
                force,
                path_span,
519
            );
520 521
            if let Err(Determinacy::Undetermined) = binding {
                return Err(Determinacy::Undetermined);
522
            }
523

524
            if trace {
525
                let kind = kind.expect("macro kind must be specified if tracing is enabled");
M
Mark Rousskov 已提交
526 527 528 529 530 531
                self.single_segment_macro_resolutions.push((
                    path[0].ident,
                    kind,
                    *parent_scope,
                    binding.ok(),
                ));
532
            }
533

534 535 536
            let res = binding.map(|binding| binding.res());
            self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
            res
537 538 539
        };

        res.map(|res| (self.get_macro(res), res))
540
    }
541

542
    // Resolve an identifier in lexical scope.
543 544
    // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
    // expansion and import resolution (perhaps they can be merged in the future).
545
    // The function is used for resolving initial segments of macro paths (e.g., `foo` in
546
    // `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
547
    crate fn early_resolve_ident_in_lexical_scope(
548
        &mut self,
549
        orig_ident: Ident,
550
        scope_set: ScopeSet,
551
        parent_scope: &ParentScope<'a>,
552 553
        record_used: bool,
        force: bool,
554
        path_span: Span,
555
    ) -> Result<&'a NameBinding<'a>, Determinacy> {
T
Taiki Endo 已提交
556
        bitflags::bitflags! {
557
            struct Flags: u8 {
558 559 560 561 562 563
                const MACRO_RULES          = 1 << 0;
                const MODULE               = 1 << 1;
                const DERIVE_HELPER_COMPAT = 1 << 2;
                const MISC_SUGGEST_CRATE   = 1 << 3;
                const MISC_SUGGEST_SELF    = 1 << 4;
                const MISC_FROM_PRELUDE    = 1 << 5;
564 565
            }
        }
566

V
Vadim Petrochenkov 已提交
567
        assert!(force || !record_used); // `record_used` implies `force`
568

569
        // Make sure `self`, `super` etc produce an error when passed to here.
570
        if orig_ident.is_path_segment_keyword() {
571 572 573
            return Err(Determinacy::Determined);
        }

574
        let (ns, macro_kind, is_import) = match scope_set {
575
            ScopeSet::All(ns, is_import) => (ns, None, is_import),
576 577
            ScopeSet::AbsolutePath(ns) => (ns, None, false),
            ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
578 579
        };

580 581 582 583
        // This is *the* result, resolution from the scope closest to the resolved identifier.
        // However, sometimes this result is "weak" because it comes from a glob import or
        // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
        // mod m { ... } // solution in outer scope
584
        // {
585 586
        //     use prefix::*; // imports another `m` - innermost solution
        //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
587 588
        //     m::mac!();
        // }
589 590
        // So we have to save the innermost solution and continue searching in outer scopes
        // to detect potential ambiguities.
T
Taiki Endo 已提交
591
        let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
592
        let mut determinacy = Determinacy::Determined;
593 594

        // Go through all the scopes and try to resolve the name.
M
Mark Rousskov 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        let break_result = self.visit_scopes(
            scope_set,
            parent_scope,
            orig_ident,
            |this, scope, use_prelude, ident| {
                let ok = |res, span, arenas| {
                    Ok((
                        (res, ty::Visibility::Public, span, ExpnId::root()).to_name_binding(arenas),
                        Flags::empty(),
                    ))
                };
                let result = match scope {
                    Scope::DeriveHelpers(expn_id) => {
                        if let Some(attr) = this
                            .helper_attrs
                            .get(&expn_id)
                            .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
                        {
                            let binding = (
                                Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
                                ty::Visibility::Public,
                                attr.span,
                                expn_id,
                            )
                                .to_name_binding(this.arenas);
                            Ok((binding, Flags::empty()))
                        } else {
                            Err(Determinacy::Determined)
                        }
624
                    }
M
Mark Rousskov 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                    Scope::DeriveHelpersCompat => {
                        let mut result = Err(Determinacy::Determined);
                        for derive in parent_scope.derives {
                            let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
                            match this.resolve_macro_path(
                                derive,
                                Some(MacroKind::Derive),
                                parent_scope,
                                true,
                                force,
                            ) {
                                Ok((Some(ext), _)) => {
                                    if ext.helper_attrs.contains(&ident.name) {
                                        let binding = (
                                            Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
                                            ty::Visibility::Public,
                                            derive.span,
                                            ExpnId::root(),
                                        )
                                            .to_name_binding(this.arenas);
                                        result = Ok((binding, Flags::DERIVE_HELPER_COMPAT));
                                        break;
                                    }
                                }
                                Ok(_) | Err(Determinacy::Determined) => {}
                                Err(Determinacy::Undetermined) => {
                                    result = Err(Determinacy::Undetermined)
                                }
653 654
                            }
                        }
M
Mark Rousskov 已提交
655
                        result
656
                    }
657 658 659 660 661
                    Scope::MacroRules(macro_rules_scope) => match macro_rules_scope {
                        MacroRulesScope::Binding(macro_rules_binding)
                            if ident == macro_rules_binding.ident =>
                        {
                            Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
662
                        }
663 664
                        MacroRulesScope::Invocation(invoc_id)
                            if !this.output_macro_rules_scopes.contains_key(&invoc_id) =>
M
Mark Rousskov 已提交
665 666 667 668 669 670 671 672 673 674
                        {
                            Err(Determinacy::Undetermined)
                        }
                        _ => Err(Determinacy::Determined),
                    },
                    Scope::CrateRoot => {
                        let root_ident = Ident::new(kw::PathRoot, ident.span);
                        let root_module = this.resolve_crate_root(root_ident);
                        let binding = this.resolve_ident_in_module_ext(
                            ModuleOrUniformRoot::Module(root_module),
675 676
                            ident,
                            ns,
677
                            parent_scope,
M
Mark Rousskov 已提交
678
                            record_used,
679
                            path_span,
M
Mark Rousskov 已提交
680 681 682 683 684 685 686 687
                        );
                        match binding {
                            Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
                            Err((Determinacy::Undetermined, Weak::No)) => {
                                return Some(Err(Determinacy::determined(force)));
                            }
                            Err((Determinacy::Undetermined, Weak::Yes)) => {
                                Err(Determinacy::Undetermined)
688
                            }
M
Mark Rousskov 已提交
689
                            Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
690 691
                        }
                    }
M
Mark Rousskov 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
                    Scope::Module(module) => {
                        let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
                        let binding = this.resolve_ident_in_module_unadjusted_ext(
                            ModuleOrUniformRoot::Module(module),
                            ident,
                            ns,
                            adjusted_parent_scope,
                            true,
                            record_used,
                            path_span,
                        );
                        match binding {
                            Ok(binding) => {
                                let misc_flags = if ptr::eq(module, this.graph_root) {
                                    Flags::MISC_SUGGEST_CRATE
                                } else if module.is_normal() {
                                    Flags::MISC_SUGGEST_SELF
                                } else {
                                    Flags::empty()
                                };
                                Ok((binding, Flags::MODULE | misc_flags))
                            }
                            Err((Determinacy::Undetermined, Weak::No)) => {
                                return Some(Err(Determinacy::determined(force)));
                            }
                            Err((Determinacy::Undetermined, Weak::Yes)) => {
                                Err(Determinacy::Undetermined)
                            }
                            Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
                        }
                    }
                    Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
                        Some(ident) => ok(
                            Res::NonMacroAttr(NonMacroAttrKind::Registered),
                            ident.span,
                            this.arenas,
                        ),
                        None => Err(Determinacy::Determined),
                    },
                    Scope::MacroUsePrelude => {
                        match this.macro_use_prelude.get(&ident.name).cloned() {
                            Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
                            None => Err(Determinacy::determined(
                                this.graph_root.unexpanded_invocations.borrow().is_empty(),
                            )),
                        }
                    }
                    Scope::BuiltinAttrs => {
                        if is_builtin_attr_name(ident.name) {
                            ok(Res::NonMacroAttr(NonMacroAttrKind::Builtin), DUMMY_SP, this.arenas)
                        } else {
                            Err(Determinacy::Determined)
                        }
                    }
                    Scope::ExternPrelude => match this.extern_prelude_get(ident, !record_used) {
                        Some(binding) => Ok((binding, Flags::empty())),
                        None => Err(Determinacy::determined(
                            this.graph_root.unexpanded_invocations.borrow().is_empty(),
                        )),
                    },
                    Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
                        Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
                        None => Err(Determinacy::Determined),
                    },
                    Scope::StdLibPrelude => {
                        let mut result = Err(Determinacy::Determined);
                        if let Some(prelude) = this.prelude {
                            if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
                                ModuleOrUniformRoot::Module(prelude),
                                ident,
                                ns,
                                parent_scope,
                                false,
                                path_span,
                            ) {
                                if use_prelude || this.is_builtin_macro(binding.res()) {
                                    result = Ok((binding, Flags::MISC_FROM_PRELUDE));
                                }
                            }
                        }
                        result
773
                    }
M
Mark Rousskov 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
                    Scope::BuiltinTypes => {
                        match this.primitive_type_table.primitive_types.get(&ident.name).cloned() {
                            Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
                            None => Err(Determinacy::Determined),
                        }
                    }
                };

                match result {
                    Ok((binding, flags))
                        if sub_namespace_match(binding.macro_kind(), macro_kind) =>
                    {
                        if !record_used {
                            return Some(Ok(binding));
                        }

                        if let Some((innermost_binding, innermost_flags)) = innermost_result {
                            // Found another solution, if the first one was "weak", report an error.
                            let (res, innermost_res) = (binding.res(), innermost_binding.res());
                            if res != innermost_res {
                                let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
                                let is_derive_helper_compat = |res, flags: Flags| {
                                    res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper)
                                        && flags.contains(Flags::DERIVE_HELPER_COMPAT)
                                };
799

M
Mark Rousskov 已提交
800 801 802 803 804 805 806 807 808 809
                                let ambiguity_error_kind = if is_import {
                                    Some(AmbiguityKind::Import)
                                } else if innermost_res == builtin || res == builtin {
                                    Some(AmbiguityKind::BuiltinAttr)
                                } else if is_derive_helper_compat(innermost_res, innermost_flags)
                                    || is_derive_helper_compat(res, flags)
                                {
                                    Some(AmbiguityKind::DeriveHelper)
                                } else if innermost_flags.contains(Flags::MACRO_RULES)
                                    && flags.contains(Flags::MODULE)
810 811 812 813
                                    && !this.disambiguate_macro_rules_vs_modularized(
                                        innermost_binding,
                                        binding,
                                    )
M
Mark Rousskov 已提交
814 815
                                    || flags.contains(Flags::MACRO_RULES)
                                        && innermost_flags.contains(Flags::MODULE)
816
                                        && !this.disambiguate_macro_rules_vs_modularized(
M
Mark Rousskov 已提交
817 818 819 820
                                            binding,
                                            innermost_binding,
                                        )
                                {
821
                                    Some(AmbiguityKind::MacroRulesVsModularized)
M
Mark Rousskov 已提交
822 823 824 825 826 827
                                } else if innermost_binding.is_glob_import() {
                                    Some(AmbiguityKind::GlobVsOuter)
                                } else if innermost_binding
                                    .may_appear_after(parent_scope.expansion, binding)
                                {
                                    Some(AmbiguityKind::MoreExpandedVsOuter)
828
                                } else {
M
Mark Rousskov 已提交
829
                                    None
830
                                };
M
Mark Rousskov 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
                                if let Some(kind) = ambiguity_error_kind {
                                    let misc = |f: Flags| {
                                        if f.contains(Flags::MISC_SUGGEST_CRATE) {
                                            AmbiguityErrorMisc::SuggestCrate
                                        } else if f.contains(Flags::MISC_SUGGEST_SELF) {
                                            AmbiguityErrorMisc::SuggestSelf
                                        } else if f.contains(Flags::MISC_FROM_PRELUDE) {
                                            AmbiguityErrorMisc::FromPrelude
                                        } else {
                                            AmbiguityErrorMisc::None
                                        }
                                    };
                                    this.ambiguity_errors.push(AmbiguityError {
                                        kind,
                                        ident: orig_ident,
                                        b1: innermost_binding,
                                        b2: binding,
                                        misc1: misc(innermost_flags),
                                        misc2: misc(flags),
                                    });
                                    return Some(Ok(innermost_binding));
                                }
853
                            }
M
Mark Rousskov 已提交
854 855 856
                        } else {
                            // Found the first solution.
                            innermost_result = Some((binding, flags));
857
                        }
858
                    }
M
Mark Rousskov 已提交
859 860
                    Ok(..) | Err(Determinacy::Determined) => {}
                    Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
861
                }
862

M
Mark Rousskov 已提交
863 864 865
                None
            },
        );
866

867 868
        if let Some(break_result) = break_result {
            return break_result;
869
        }
870

871
        // The first found solution was the only one, return it.
872
        if let Some((binding, _)) = innermost_result {
873
            return Ok(binding);
874
        }
875

876
        Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
877 878
    }

879
    crate fn finalize_macro_resolutions(&mut self) {
M
Mark Rousskov 已提交
880 881 882 883 884 885
        let check_consistency = |this: &mut Self,
                                 path: &[Segment],
                                 span,
                                 kind: MacroKind,
                                 initial_res: Option<Res>,
                                 res: Res| {
886 887
            if let Some(initial_res) = initial_res {
                if res != initial_res && res != Res::Err && this.ambiguity_errors.is_empty() {
888 889 890
                    // Make sure compilation does not succeed if preferred macro resolution
                    // has changed after the macro had been expanded. In theory all such
                    // situations should be reported as ambiguity errors, so this is a bug.
891
                    span_bug!(span, "inconsistent resolution for a macro");
892 893 894 895 896 897 898 899 900 901
                }
            } else {
                // It's possible that the macro was unresolved (indeterminate) and silently
                // expanded into a dummy fragment for recovery during expansion.
                // Now, post-expansion, the resolution may succeed, but we can't change the
                // past and need to report an error.
                // However, non-speculative `resolve_path` can successfully return private items
                // even if speculative `resolve_path` returned nothing previously, so we skip this
                // less informative error if the privacy error is reported elsewhere.
                if this.privacy_errors.is_empty() {
M
Mark Rousskov 已提交
902 903 904 905 906
                    let msg = format!(
                        "cannot determine resolution for the {} `{}`",
                        kind.descr(),
                        Segment::names_to_string(path)
                    );
907 908 909 910 911 912
                    let msg_note = "import resolution is stuck, try simplifying macro imports";
                    this.session.struct_span_err(span, &msg).note(msg_note).emit();
                }
            }
        };

913
        let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions);
914
        for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
915
            // FIXME: Path resolution will ICE if segment IDs present.
M
Mark Rousskov 已提交
916 917 918
            for seg in &mut path {
                seg.id = None;
            }
919
            match self.resolve_path(
M
Mark Rousskov 已提交
920 921 922 923 924 925
                &path,
                Some(MacroNS),
                &parent_scope,
                true,
                path_span,
                CrateLint::No,
926
            ) {
927
                PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
928 929
                    let res = path_res.base_res();
                    check_consistency(self, &path, path_span, kind, initial_res, res);
930
                }
931 932 933
                path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => {
                    let (span, label) = if let PathResult::Failed { span, label, .. } = path_res {
                        (span, label)
934
                    } else {
M
Mark Rousskov 已提交
935 936 937 938 939 940 941 942
                        (
                            path_span,
                            format!(
                                "partially resolved path in {} {}",
                                kind.article(),
                                kind.descr()
                            ),
                        )
943
                    };
M
Mark Rousskov 已提交
944 945 946 947
                    self.report_error(
                        span,
                        ResolutionError::FailedToResolve { label, suggestion: None },
                    );
948
                }
949
                PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
950 951 952
            }
        }

953
        let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions);
954
        for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
M
Mark Rousskov 已提交
955 956 957 958 959 960 961 962
            match self.early_resolve_ident_in_lexical_scope(
                ident,
                ScopeSet::Macro(kind),
                &parent_scope,
                true,
                true,
                ident.span,
            ) {
963
                Ok(binding) => {
964
                    let initial_res = initial_binding.map(|initial_binding| {
965
                        self.record_use(ident, MacroNS, initial_binding, false);
966
                        initial_binding.res()
967
                    });
968
                    let res = binding.res();
V
Vadim Petrochenkov 已提交
969
                    let seg = Segment::from_ident(ident);
970
                    check_consistency(self, &[seg], ident.span, kind, initial_res, res);
971
                }
972
                Err(..) => {
973
                    let expected = kind.descr_expected();
974
                    let msg = format!("cannot find {} `{}` in this scope", expected, ident);
975
                    let mut err = self.session.struct_span_err(ident.span, &msg);
976
                    self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
977
                    err.emit();
978
                }
979
            }
J
Jeffrey Seyfried 已提交
980
        }
981

982
        let builtin_attrs = mem::take(&mut self.builtin_attrs);
983
        for (ident, parent_scope) in builtin_attrs {
984
            let _ = self.early_resolve_ident_in_lexical_scope(
M
Mark Rousskov 已提交
985 986 987 988 989 990
                ident,
                ScopeSet::Macro(MacroKind::Attr),
                &parent_scope,
                true,
                true,
                ident.span,
991
            );
992
        }
993
    }
994

995 996 997 998 999 1000
    fn check_stability_and_deprecation(
        &mut self,
        ext: &SyntaxExtension,
        path: &ast::Path,
        node_id: NodeId,
    ) {
1001
        let span = path.span;
1002
        if let Some(stability) = &ext.stability {
1003
            if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
1004 1005
                let feature = stability.feature;
                if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
1006
                    let lint_buffer = &mut self.lint_buffer;
M
Mark Rousskov 已提交
1007 1008
                    let soft_handler =
                        |lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);
1009
                    stability::report_unstable(
M
Mark Rousskov 已提交
1010 1011 1012 1013 1014 1015 1016
                        self.session,
                        feature,
                        reason,
                        issue,
                        is_soft,
                        span,
                        soft_handler,
1017
                    );
1018 1019 1020 1021
                }
            }
        }
        if let Some(depr) = &ext.deprecation {
1022
            let path = pprust::path_to_string(&path);
1023
            let (message, lint) = stability::deprecation_message(depr, "macro", &path);
1024 1025 1026 1027 1028 1029 1030
            stability::early_report_deprecation(
                &mut self.lint_buffer,
                &message,
                depr.suggestion,
                lint,
                span,
            );
1031 1032 1033
        }
    }

M
Mark Rousskov 已提交
1034 1035 1036 1037 1038 1039
    fn prohibit_imported_non_macro_attrs(
        &self,
        binding: Option<&'a NameBinding<'a>>,
        res: Option<Res>,
        span: Span,
    ) {
1040
        if let Some(Res::NonMacroAttr(kind)) = res {
1041
            if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
V
Vadim Petrochenkov 已提交
1042 1043
                let msg =
                    format!("cannot use {} {} through an import", kind.article(), kind.descr());
1044 1045 1046 1047 1048 1049 1050 1051 1052
                let mut err = self.session.struct_span_err(span, &msg);
                if let Some(binding) = binding {
                    err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
                }
                err.emit();
            }
        }
    }

1053 1054 1055 1056
    crate fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
        // Reserve some names that are not quite covered by the general check
        // performed on `Resolver::builtin_attrs`.
        if ident.name == sym::cfg || ident.name == sym::cfg_attr || ident.name == sym::derive {
1057
            let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind());
1058 1059
            if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
                self.session.span_err(
M
Mark Rousskov 已提交
1060 1061
                    ident.span,
                    &format!("name `{}` is reserved in attribute namespace", ident),
1062 1063 1064 1065 1066
                );
            }
        }
    }

1067 1068
    /// Compile the macro into a `SyntaxExtension` and possibly replace
    /// its expander to a pre-defined one for built-in macros.
1069
    crate fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> SyntaxExtension {
1070
        let mut result = compile_declarative_macro(
1071
            &self.session,
M
Mark Rousskov 已提交
1072 1073 1074
            self.session.features_untracked(),
            item,
            edition,
1075 1076 1077 1078 1079
        );

        if result.is_builtin {
            // The macro was marked with `#[rustc_builtin_macro]`.
            if let Some(ext) = self.builtin_macros.remove(&item.ident.name) {
1080 1081 1082
                // The macro is a built-in, replace its expander function
                // while still taking everything else from the source code.
                result.kind = ext.kind;
1083 1084 1085 1086 1087 1088
            } else {
                let msg = format!("cannot find a built-in macro with name `{}`", item.ident);
                self.session.span_err(item.span, &msg);
            }
        }

1089
        result
1090
    }
1091
}