Skip to content
Permalink
Browse files

expand: Do not overwrite existing `ExpnInfo` when injecting derive ma…

…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 6, 2019
1 parent 0ec6ea7 commit d1949b1ab01dbd482008f64af54161cc43bb0991
Showing with 9 additions and 4 deletions.
  1. +4 −3 src/libsyntax/ext/derive.rs
  2. +5 −1 src/libsyntax_pos/hygiene.rs
@@ -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> {
@@ -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);
@@ -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 {

0 comments on commit d1949b1

Please sign in to comment.
You can’t perform that action at this time.