Skip to content

Commit ea68445

Browse files
committed
Move in_external_macro to SyntaxContext
1 parent 25cdf1f commit ea68445

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

compiler/rustc_span/src/hygiene.rs

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use tracing::{debug, trace};
4141

4242
use crate::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, StableCrateId};
4343
use crate::edition::Edition;
44+
use crate::source_map::SourceMap;
4445
use crate::symbol::{Symbol, kw, sym};
4546
use crate::{DUMMY_SP, HashStableContext, Span, SpanDecoder, SpanEncoder, with_session_globals};
4647

@@ -907,6 +908,30 @@ impl SyntaxContext {
907908
pub fn edition(self) -> Edition {
908909
HygieneData::with(|data| data.expn_data(data.outer_expn(self)).edition)
909910
}
911+
912+
/// Returns whether this context originates in a foreign crate's external macro.
913+
///
914+
/// This is used to test whether a lint should not even begin to figure out whether it should
915+
/// be reported on the current node.
916+
pub fn in_external_macro(self, sm: &SourceMap) -> bool {
917+
let expn_data = self.outer_expn_data();
918+
match expn_data.kind {
919+
ExpnKind::Root
920+
| ExpnKind::Desugaring(
921+
DesugaringKind::ForLoop
922+
| DesugaringKind::WhileLoop
923+
| DesugaringKind::OpaqueTy
924+
| DesugaringKind::Async
925+
| DesugaringKind::Await,
926+
) => false,
927+
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
928+
ExpnKind::Macro(MacroKind::Bang, _) => {
929+
// Dummy span for the `def_site` means it's an external macro.
930+
expn_data.def_site.is_dummy() || sm.is_imported(expn_data.def_site)
931+
}
932+
ExpnKind::Macro { .. } => true, // definitely a plugin
933+
}
934+
}
910935
}
911936

912937
impl fmt::Debug for SyntaxContext {

compiler/rustc_span/src/lib.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -607,28 +607,13 @@ impl Span {
607607
!self.is_dummy() && sm.is_span_accessible(self)
608608
}
609609

610-
/// Returns whether `span` originates in a foreign crate's external macro.
610+
/// Returns whether this span originates in a foreign crate's external macro.
611611
///
612612
/// This is used to test whether a lint should not even begin to figure out whether it should
613613
/// be reported on the current node.
614+
#[inline]
614615
pub fn in_external_macro(self, sm: &SourceMap) -> bool {
615-
let expn_data = self.ctxt().outer_expn_data();
616-
match expn_data.kind {
617-
ExpnKind::Root
618-
| ExpnKind::Desugaring(
619-
DesugaringKind::ForLoop
620-
| DesugaringKind::WhileLoop
621-
| DesugaringKind::OpaqueTy
622-
| DesugaringKind::Async
623-
| DesugaringKind::Await,
624-
) => false,
625-
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
626-
ExpnKind::Macro(MacroKind::Bang, _) => {
627-
// Dummy span for the `def_site` means it's an external macro.
628-
expn_data.def_site.is_dummy() || sm.is_imported(expn_data.def_site)
629-
}
630-
ExpnKind::Macro { .. } => true, // definitely a plugin
631-
}
616+
self.ctxt().in_external_macro(sm)
632617
}
633618

634619
/// Returns `true` if `span` originates in a derive-macro's expansion.

0 commit comments

Comments
 (0)