Skip to content

Commit

Permalink
expand: Do not overwrite existing ExpnInfo when injecting derive ma…
Browse files Browse the repository at this point in the history
…rkers

Create a fresh expansion for them instead - this is the usual way to allow unstable features for generated/desugared code.
Fixes #52363
  • Loading branch information
petrochenkov committed Jul 10, 2019
1 parent 0ec6ea7 commit d1949b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/libsyntax/ext/derive.rs
Expand Up @@ -8,7 +8,7 @@ use crate::symbol::{Symbol, sym};
use crate::errors::Applicability;

use syntax_pos::Span;

use syntax_pos::hygiene::{Mark, SyntaxContext};
use rustc_data_structures::fx::FxHashSet;

pub fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>) -> Vec<ast::Path> {
Expand Down Expand Up @@ -55,12 +55,13 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
names.insert(unwrap_or!(path.segments.get(0), continue).ident.name);
}

cx.current_expansion.mark.set_expn_info(ExpnInfo::with_unstable(
let mark = Mark::fresh(cx.current_expansion.mark);
mark.set_expn_info(ExpnInfo::with_unstable(
ExpnKind::Macro(MacroKind::Derive, Symbol::intern(&pretty_name)), span,
cx.parse_sess.edition, &[sym::rustc_attrs, sym::structural_match],
));

let span = span.with_ctxt(cx.backtrace());
let span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
item.visit_attrs(|attrs| {
if names.contains(&sym::Eq) && names.contains(&sym::PartialEq) {
let meta = cx.meta_word(span, sym::structural_match);
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax_pos/hygiene.rs
Expand Up @@ -117,7 +117,11 @@ impl Mark {

#[inline]
pub fn set_expn_info(self, info: ExpnInfo) {
HygieneData::with(|data| data.marks[self.0 as usize].expn_info = Some(info))
HygieneData::with(|data| {
let old_info = &mut data.marks[self.0 as usize].expn_info;
assert!(old_info.is_none(), "expansion info is reset for a mark");
*old_info = Some(info);
})
}

pub fn is_descendant_of(self, ancestor: Mark) -> bool {
Expand Down

0 comments on commit d1949b1

Please sign in to comment.