Skip to content

Commit 73dee25

Browse files
committed
hygiene: Remove Options from functions returning ExpnInfo
The expansion info is not optional and should always exist
1 parent 6cb28b6 commit 73dee25

File tree

14 files changed

+98
-155
lines changed

14 files changed

+98
-155
lines changed

src/librustc/lint/internal.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use errors::Applicability;
99
use rustc_data_structures::fx::FxHashMap;
1010
use syntax::ast::{Ident, Item, ItemKind};
1111
use syntax::symbol::{sym, Symbol};
12-
use syntax_pos::ExpnInfo;
1312

1413
declare_tool_lint! {
1514
pub rustc::DEFAULT_HASH_TYPES,
@@ -228,30 +227,20 @@ impl EarlyLintPass for LintPassImpl {
228227
if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.node {
229228
if let Some(last) = lint_pass.path.segments.last() {
230229
if last.ident.name == sym::LintPass {
231-
match &lint_pass.path.span.ctxt().outer_expn_info() {
232-
Some(info) if is_lint_pass_expansion(info) => {}
233-
_ => {
234-
cx.struct_span_lint(
235-
LINT_PASS_IMPL_WITHOUT_MACRO,
236-
lint_pass.path.span,
237-
"implementing `LintPass` by hand",
238-
)
239-
.help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
240-
.emit();
241-
}
230+
let expn_info = lint_pass.path.span.ctxt().outer_expn_info();
231+
let call_site = expn_info.call_site;
232+
if expn_info.kind.descr() != sym::impl_lint_pass &&
233+
call_site.ctxt().outer_expn_info().kind.descr() != sym::declare_lint_pass {
234+
cx.struct_span_lint(
235+
LINT_PASS_IMPL_WITHOUT_MACRO,
236+
lint_pass.path.span,
237+
"implementing `LintPass` by hand",
238+
)
239+
.help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
240+
.emit();
242241
}
243242
}
244243
}
245244
}
246245
}
247246
}
248-
249-
fn is_lint_pass_expansion(expn_info: &ExpnInfo) -> bool {
250-
if expn_info.kind.descr() == sym::impl_lint_pass {
251-
true
252-
} else if let Some(info) = expn_info.call_site.ctxt().outer_expn_info() {
253-
info.kind.descr() == sym::declare_lint_pass
254-
} else {
255-
false
256-
}
257-
}

src/librustc/lint/mod.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -885,21 +885,16 @@ pub fn provide(providers: &mut Providers<'_>) {
885885
/// This is used to test whether a lint should not even begin to figure out whether it should
886886
/// be reported on the current node.
887887
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
888-
let info = match span.ctxt().outer_expn_info() {
889-
Some(info) => info,
890-
// no ExpnInfo means this span doesn't come from a macro
891-
None => return false,
892-
};
893-
894-
match info.kind {
888+
let expn_info = span.ctxt().outer_expn_info();
889+
match expn_info.kind {
895890
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
896891
ExpnKind::Desugaring(_) => true, // well, it's "external"
897892
ExpnKind::Macro(MacroKind::Bang, _) => {
898-
if info.def_site.is_dummy() {
893+
if expn_info.def_site.is_dummy() {
899894
// dummy span for the def_site means it's an external macro
900895
return true;
901896
}
902-
match sess.source_map().span_to_snippet(info.def_site) {
897+
match sess.source_map().span_to_snippet(expn_info.def_site) {
903898
Ok(code) => !code.starts_with("macro_rules"),
904899
// no snippet = external macro or compiler-builtin expansion
905900
Err(_) => true,
@@ -911,10 +906,8 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
911906

912907
/// Returns whether `span` originates in a derive macro's expansion
913908
pub fn in_derive_expansion(span: Span) -> bool {
914-
if let Some(info) = span.ctxt().outer_expn_info() {
915-
if let ExpnKind::Macro(MacroKind::Derive, _) = info.kind {
916-
return true;
917-
}
909+
if let ExpnKind::Macro(MacroKind::Derive, _) = span.ctxt().outer_expn_info().kind {
910+
return true;
918911
}
919912
false
920913
}

src/librustc/traits/error_reporting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use errors::{Applicability, DiagnosticBuilder};
3636
use std::fmt;
3737
use syntax::ast;
3838
use syntax::symbol::sym;
39-
use syntax_pos::{DUMMY_SP, Span, ExpnInfo, ExpnKind};
39+
use syntax_pos::{DUMMY_SP, Span, ExpnKind};
4040

4141
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
4242
pub fn report_fulfillment_errors(&self,
@@ -61,9 +61,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
6161
// We want to ignore desugarings here: spans are equivalent even
6262
// if one is the result of a desugaring and the other is not.
6363
let mut span = error.obligation.cause.span;
64-
if let Some(ExpnInfo { kind: ExpnKind::Desugaring(_), def_site, .. })
65-
= span.ctxt().outer_expn_info() {
66-
span = def_site;
64+
let expn_info = span.ctxt().outer_expn_info();
65+
if let ExpnKind::Desugaring(_) = expn_info.kind {
66+
span = expn_info.call_site;
6767
}
6868

6969
error_map.entry(span).or_default().push(

src/librustc/ty/query/on_disk_cache.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -820,18 +820,14 @@ where
820820
TAG_NO_EXPANSION_INFO.encode(self)
821821
} else {
822822
let (expn_id, expn_info) = span_data.ctxt.outer_expn_with_info();
823-
if let Some(expn_info) = expn_info {
824-
if let Some(pos) = self.expn_info_shorthands.get(&expn_id).cloned() {
825-
TAG_EXPANSION_INFO_SHORTHAND.encode(self)?;
826-
pos.encode(self)
827-
} else {
828-
TAG_EXPANSION_INFO_INLINE.encode(self)?;
829-
let pos = AbsoluteBytePos::new(self.position());
830-
self.expn_info_shorthands.insert(expn_id, pos);
831-
expn_info.encode(self)
832-
}
823+
if let Some(pos) = self.expn_info_shorthands.get(&expn_id).cloned() {
824+
TAG_EXPANSION_INFO_SHORTHAND.encode(self)?;
825+
pos.encode(self)
833826
} else {
834-
TAG_NO_EXPANSION_INFO.encode(self)
827+
TAG_EXPANSION_INFO_INLINE.encode(self)?;
828+
let pos = AbsoluteBytePos::new(self.position());
829+
self.expn_info_shorthands.insert(expn_id, pos);
830+
expn_info.encode(self)
835831
}
836832
}
837833
}

src/librustc_codegen_ssa/back/write.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,10 +1775,7 @@ impl SharedEmitterMain {
17751775
}
17761776
}
17771777
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg)) => {
1778-
match ExpnId::from_u32(cookie).expn_info() {
1779-
Some(ei) => sess.span_err(ei.call_site, &msg),
1780-
None => sess.err(&msg),
1781-
}
1778+
sess.span_err(ExpnId::from_u32(cookie).expn_info().call_site, &msg)
17821779
}
17831780
Ok(SharedEmitterMessage::AbortIfErrors) => {
17841781
sess.abort_if_errors();

src/librustc_lint/unused.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,8 @@ impl EarlyLintPass for UnusedParens {
517517
// trigger in situations that macro authors shouldn't have to care about, e.g.,
518518
// when a parenthesized token tree matched in one macro expansion is matched as
519519
// an expression in another and used as a fn/method argument (Issue #47775)
520-
if e.span.ctxt().outer_expn_info()
521-
.map_or(false, |info| info.call_site.from_expansion()) {
522-
return;
520+
if e.span.ctxt().outer_expn_info().call_site.from_expansion() {
521+
return;
523522
}
524523
let msg = format!("{} argument", call_kind);
525524
for arg in args_to_check {

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ impl<'a> Resolver<'a> {
346346

347347
// Possibly apply the macro helper hack
348348
if kind == Some(MacroKind::Bang) && path.len() == 1 &&
349-
path[0].ident.span.ctxt().outer_expn_info()
350-
.map_or(false, |info| info.local_inner_macros) {
349+
path[0].ident.span.ctxt().outer_expn_info().local_inner_macros {
351350
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
352351
path.insert(0, Segment::from_ident(root));
353352
}

src/libsyntax/ext/base.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -756,10 +756,7 @@ impl<'a> ExtCtxt<'a> {
756756
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
757757
pub fn cfg(&self) -> &ast::CrateConfig { &self.parse_sess.config }
758758
pub fn call_site(&self) -> Span {
759-
match self.current_expansion.id.expn_info() {
760-
Some(expn_info) => expn_info.call_site,
761-
None => DUMMY_SP,
762-
}
759+
self.current_expansion.id.expn_info().call_site
763760
}
764761
pub fn backtrace(&self) -> SyntaxContext {
765762
SyntaxContext::root().apply_mark(self.current_expansion.id)
@@ -772,17 +769,13 @@ impl<'a> ExtCtxt<'a> {
772769
let mut ctxt = self.backtrace();
773770
let mut last_macro = None;
774771
loop {
775-
if ctxt.outer_expn_info().map_or(None, |info| {
776-
if info.kind.descr() == sym::include {
777-
// Stop going up the backtrace once include! is encountered
778-
return None;
779-
}
780-
ctxt = info.call_site.ctxt();
781-
last_macro = Some(info.call_site);
782-
Some(())
783-
}).is_none() {
784-
break
772+
let expn_info = ctxt.outer_expn_info();
773+
// Stop going up the backtrace once include! is encountered
774+
if expn_info.is_root() || expn_info.kind.descr() == sym::include {
775+
break;
785776
}
777+
ctxt = expn_info.call_site.ctxt();
778+
last_macro = Some(expn_info.call_site);
786779
}
787780
last_macro
788781
}

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
475475
}
476476

477477
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
478-
let info = self.cx.current_expansion.id.expn_info().unwrap();
478+
let info = self.cx.current_expansion.id.expn_info();
479479
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
480480
let mut err = self.cx.struct_span_err(info.call_site,
481481
&format!("recursion limit reached while expanding the macro `{}`",

src/libsyntax/ext/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ pub(crate) struct Rustc<'a> {
362362
impl<'a> Rustc<'a> {
363363
pub fn new(cx: &'a ExtCtxt<'_>) -> Self {
364364
// No way to determine def location for a proc macro right now, so use call location.
365-
let location = cx.current_expansion.id.expn_info().unwrap().call_site;
365+
let location = cx.current_expansion.id.expn_info().call_site;
366366
let to_span = |transparency| {
367367
location.with_ctxt(
368368
SyntaxContext::root()
@@ -677,7 +677,7 @@ impl server::Span for Rustc<'_> {
677677
self.sess.source_map().lookup_char_pos(span.lo()).file
678678
}
679679
fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
680-
span.ctxt().outer_expn_info().map(|i| i.call_site)
680+
span.parent()
681681
}
682682
fn source(&mut self, span: Self::Span) -> Self::Span {
683683
span.source_callsite()

src/libsyntax/parse/parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod generics;
1313
use crate::ast::{self, AttrStyle, Attribute, Arg, BindingMode, StrStyle, SelfKind};
1414
use crate::ast::{FnDecl, Ident, IsAsync, MacDelimiter, Mutability, TyKind};
1515
use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar};
16-
use crate::ext::hygiene::SyntaxContext;
1716
use crate::source_map::{self, respan};
1817
use crate::parse::{SeqSep, literal, token};
1918
use crate::parse::lexer::UnmatchedBrace;

src/libsyntax/source_map.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ mod tests;
3131
/// otherwise return the call site span up to the `enclosing_sp` by
3232
/// following the `expn_info` chain.
3333
pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
34-
let call_site1 = sp.ctxt().outer_expn_info().map(|ei| ei.call_site);
35-
let call_site2 = enclosing_sp.ctxt().outer_expn_info().map(|ei| ei.call_site);
36-
match (call_site1, call_site2) {
37-
(None, _) => sp,
38-
(Some(call_site1), Some(call_site2)) if call_site1 == call_site2 => sp,
39-
(Some(call_site1), _) => original_sp(call_site1, enclosing_sp),
34+
let expn_info1 = sp.ctxt().outer_expn_info();
35+
let expn_info2 = enclosing_sp.ctxt().outer_expn_info();
36+
if expn_info1.is_root() ||
37+
!expn_info2.is_root() && expn_info1.call_site == expn_info2.call_site {
38+
sp
39+
} else {
40+
original_sp(expn_info1.call_site, enclosing_sp)
4041
}
4142
}
4243

src/libsyntax_pos/hygiene.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl ExpnId {
112112
}
113113

114114
#[inline]
115-
pub fn expn_info(self) -> Option<ExpnInfo> {
116-
HygieneData::with(|data| data.expn_info(self).cloned())
115+
pub fn expn_info(self) -> ExpnInfo {
116+
HygieneData::with(|data| data.expn_info(self).clone())
117117
}
118118

119119
#[inline]
@@ -139,12 +139,9 @@ impl ExpnId {
139139
#[inline]
140140
pub fn looks_like_proc_macro_derive(self) -> bool {
141141
HygieneData::with(|data| {
142-
if data.default_transparency(self) == Transparency::Opaque {
143-
if let Some(expn_info) = data.expn_info(self) {
144-
if let ExpnKind::Macro(MacroKind::Derive, _) = expn_info.kind {
145-
return true;
146-
}
147-
}
142+
let expn_info = data.expn_info(self);
143+
if let ExpnKind::Macro(MacroKind::Derive, _) = expn_info.kind {
144+
return expn_info.default_transparency == Transparency::Opaque;
148145
}
149146
false
150147
})
@@ -190,16 +187,9 @@ impl HygieneData {
190187
self.expn_data[expn_id.0 as usize].parent
191188
}
192189

193-
fn expn_info(&self, expn_id: ExpnId) -> Option<&ExpnInfo> {
194-
if expn_id != ExpnId::root() {
195-
Some(self.expn_data[expn_id.0 as usize].expn_info.as_ref()
196-
.expect("no expansion info for an expansion ID"))
197-
} else {
198-
// FIXME: Some code relies on `expn_info().is_none()` meaning "no expansion".
199-
// Introduce a method for checking for "no expansion" instead and always return
200-
// `ExpnInfo` from this function instead of the `Option`.
201-
None
202-
}
190+
fn expn_info(&self, expn_id: ExpnId) -> &ExpnInfo {
191+
self.expn_data[expn_id.0 as usize].expn_info.as_ref()
192+
.expect("no expansion info for an expansion ID")
203193
}
204194

205195
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
@@ -212,12 +202,6 @@ impl HygieneData {
212202
true
213203
}
214204

215-
fn default_transparency(&self, expn_id: ExpnId) -> Transparency {
216-
self.expn_info(expn_id).map_or(
217-
Transparency::SemiTransparent, |einfo| einfo.default_transparency
218-
)
219-
}
220-
221205
fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
222206
self.syntax_context_data[ctxt.0 as usize].opaque
223207
}
@@ -256,11 +240,7 @@ impl HygieneData {
256240

257241
fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
258242
while span.from_expansion() && span.ctxt() != to {
259-
if let Some(info) = self.expn_info(self.outer_expn(span.ctxt())) {
260-
span = info.call_site;
261-
} else {
262-
break;
263-
}
243+
span = self.expn_info(self.outer_expn(span.ctxt())).call_site;
264244
}
265245
span
266246
}
@@ -275,7 +255,9 @@ impl HygieneData {
275255

276256
fn apply_mark(&mut self, ctxt: SyntaxContext, expn_id: ExpnId) -> SyntaxContext {
277257
assert_ne!(expn_id, ExpnId::root());
278-
self.apply_mark_with_transparency(ctxt, expn_id, self.default_transparency(expn_id))
258+
self.apply_mark_with_transparency(
259+
ctxt, expn_id, self.expn_info(expn_id).default_transparency
260+
)
279261
}
280262

281263
fn apply_mark_with_transparency(&mut self, ctxt: SyntaxContext, expn_id: ExpnId,
@@ -285,8 +267,7 @@ impl HygieneData {
285267
return self.apply_mark_internal(ctxt, expn_id, transparency);
286268
}
287269

288-
let call_site_ctxt =
289-
self.expn_info(expn_id).map_or(SyntaxContext::root(), |info| info.call_site.ctxt());
270+
let call_site_ctxt = self.expn_info(expn_id).call_site.ctxt();
290271
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
291272
self.modern(call_site_ctxt)
292273
} else {
@@ -581,17 +562,17 @@ impl SyntaxContext {
581562
/// `ctxt.outer_expn_info()` is equivalent to but faster than
582563
/// `ctxt.outer_expn().expn_info()`.
583564
#[inline]
584-
pub fn outer_expn_info(self) -> Option<ExpnInfo> {
585-
HygieneData::with(|data| data.expn_info(data.outer_expn(self)).cloned())
565+
pub fn outer_expn_info(self) -> ExpnInfo {
566+
HygieneData::with(|data| data.expn_info(data.outer_expn(self)).clone())
586567
}
587568

588569
/// `ctxt.outer_expn_with_info()` is equivalent to but faster than
589570
/// `{ let outer = ctxt.outer_expn(); (outer, outer.expn_info()) }`.
590571
#[inline]
591-
pub fn outer_expn_with_info(self) -> (ExpnId, Option<ExpnInfo>) {
572+
pub fn outer_expn_with_info(self) -> (ExpnId, ExpnInfo) {
592573
HygieneData::with(|data| {
593574
let outer = data.outer_expn(self);
594-
(outer, data.expn_info(outer).cloned())
575+
(outer, data.expn_info(outer).clone())
595576
})
596577
}
597578

@@ -681,6 +662,11 @@ impl ExpnInfo {
681662
..ExpnInfo::default(kind, call_site, edition)
682663
}
683664
}
665+
666+
#[inline]
667+
pub fn is_root(&self) -> bool {
668+
if let ExpnKind::Root = self.kind { true } else { false }
669+
}
684670
}
685671

686672
/// Expansion kind.

0 commit comments

Comments
 (0)