Skip to content

Commit e152554

Browse files
committed
resolve/expand: Move expansion info setting to a single earlier point
1 parent 1ff3bce commit e152554

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

src/librustc_resolve/macros.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKi
114114
candidate.is_none() || requirement.is_none() || candidate == requirement
115115
}
116116

117+
// We don't want to format a path using pretty-printing,
118+
// `format!("{}", path)`, because that tries to insert
119+
// line-breaks and is slow.
120+
fn fast_print_path(path: &ast::Path) -> String {
121+
let mut path_str = String::with_capacity(64);
122+
for (i, segment) in path.segments.iter().enumerate() {
123+
if i != 0 {
124+
path_str.push_str("::");
125+
}
126+
if segment.ident.name != kw::PathRoot {
127+
path_str.push_str(&segment.ident.as_str())
128+
}
129+
}
130+
path_str
131+
}
132+
117133
impl<'a> base::Resolver for Resolver<'a> {
118134
fn next_node_id(&mut self) -> ast::NodeId {
119135
self.session.next_node_id()
@@ -209,14 +225,19 @@ impl<'a> base::Resolver for Resolver<'a> {
209225
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
210226
let (res, ext) = match self.resolve_macro_to_res(path, kind, &parent_scope, true, force) {
211227
Ok((res, ext)) => (res, ext),
212-
Err(Determinacy::Determined) if kind == MacroKind::Attr => {
213-
// Replace unresolved attributes with used inert attributes for better recovery.
214-
return Ok(Some(self.non_macro_attr(true)));
215-
}
228+
// Replace unresolved attributes with used inert attributes for better recovery.
229+
Err(Determinacy::Determined) if kind == MacroKind::Attr =>
230+
(Res::Err, self.non_macro_attr(true)),
216231
Err(determinacy) => return Err(determinacy),
217232
};
218233

219-
if let Res::Def(DefKind::Macro(_), def_id) = res {
234+
let format = match kind {
235+
MacroKind::Derive => format!("derive({})", fast_print_path(path)),
236+
_ => fast_print_path(path),
237+
};
238+
invoc.expansion_data.mark.set_expn_info(ext.expn_info(invoc.span(), &format));
239+
240+
if let Res::Def(_, def_id) = res {
220241
if after_derive {
221242
self.session.span_err(invoc.span(),
222243
"macro attributes must be placed before `#[derive]`");

src/libsyntax/ext/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl SyntaxExtension {
677677
}
678678
}
679679

680-
crate fn expn_info(&self, call_site: Span, format: &str) -> ExpnInfo {
680+
pub fn expn_info(&self, call_site: Span, format: &str) -> ExpnInfo {
681681
ExpnInfo {
682682
call_site,
683683
format: self.expn_format(Symbol::intern(format)),

src/libsyntax/ext/expand.rs

+3-29
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,6 @@ impl AstFragmentKind {
188188
}
189189
}
190190

191-
// We don't want to format a path using pretty-printing,
192-
// `format!("{}", path)`, because that tries to insert
193-
// line-breaks and is slow.
194-
fn fast_print_path(path: &ast::Path) -> String {
195-
let mut path_str = String::with_capacity(64);
196-
for (i, segment) in path.segments.iter().enumerate() {
197-
if i != 0 {
198-
path_str.push_str("::");
199-
}
200-
if segment.ident.name != kw::PathRoot {
201-
path_str.push_str(&segment.ident.as_str())
202-
}
203-
}
204-
path_str
205-
}
206-
207191
pub struct Invocation {
208192
pub kind: InvocationKind,
209193
fragment_kind: AstFragmentKind,
@@ -546,9 +530,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
546530
_ => unreachable!(),
547531
};
548532

549-
let expn_info = ext.expn_info(attr.span, &fast_print_path(&attr.path));
550-
invoc.expansion_data.mark.set_expn_info(expn_info);
551-
552533
match &ext.kind {
553534
SyntaxExtensionKind::NonMacroAttr { mark_used } => {
554535
attr::mark_known(&attr);
@@ -682,15 +663,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
682663
invoc: Invocation,
683664
ext: &SyntaxExtension)
684665
-> Option<AstFragment> {
685-
let (mark, kind) = (invoc.expansion_data.mark, invoc.fragment_kind);
666+
let kind = invoc.fragment_kind;
686667
let (mac, ident, span) = match invoc.kind {
687668
InvocationKind::Bang { mac, ident, span } => (mac, ident, span),
688669
_ => unreachable!(),
689670
};
690671
let path = &mac.node.path;
691672

692673
let ident = ident.unwrap_or_else(|| Ident::invalid());
693-
let validate_and_set_expn_info = |this: &mut Self| {
674+
let validate = |this: &mut Self| {
694675
// feature-gate the macro invocation
695676
if let Some((feature, issue)) = ext.unstable_feature {
696677
let crate_span = this.cx.current_expansion.crate_span.unwrap();
@@ -715,13 +696,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
715696
this.cx.trace_macros_diag();
716697
return Err(kind.dummy(span));
717698
}
718-
mark.set_expn_info(ext.expn_info(span, &fast_print_path(path)));
719699
Ok(())
720700
};
721701

722702
let opt_expanded = match &ext.kind {
723703
SyntaxExtensionKind::LegacyBang(expander) => {
724-
if let Err(dummy_span) = validate_and_set_expn_info(self) {
704+
if let Err(dummy_span) = validate(self) {
725705
dummy_span
726706
} else {
727707
kind.make_from(expander.expand(
@@ -757,8 +737,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
757737
kind.dummy(span)
758738
} else {
759739
self.gate_proc_macro_expansion_kind(span, kind);
760-
let expn_info = ext.expn_info(span, &fast_print_path(path));
761-
invoc.expansion_data.mark.set_expn_info(expn_info);
762740
let tok_result = expander.expand(self.cx, span, mac.node.stream());
763741
let result = self.parse_ast_fragment(tok_result, kind, path, span);
764742
self.gate_proc_macro_expansion(span, &result);
@@ -818,10 +796,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
818796
match &ext.kind {
819797
SyntaxExtensionKind::Derive(expander) |
820798
SyntaxExtensionKind::LegacyDerive(expander) => {
821-
let expn_info =
822-
ext.expn_info(path.span, &format!("derive({})", fast_print_path(&path)));
823-
invoc.expansion_data.mark.set_expn_info(expn_info);
824-
825799
let meta = ast::MetaItem { node: ast::MetaItemKind::Word, span: path.span, path };
826800
let span = meta.span.with_ctxt(self.cx.backtrace());
827801
let items = expander.expand(self.cx, span, &meta, item);

0 commit comments

Comments
 (0)