From a0e606c380ebd56f3d30501d7ccb94d9f11b9643 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sat, 28 May 2016 11:58:45 +0000 Subject: Use the span of `#[derive_Eq]` for `#[structural_match]` --- src/libsyntax_ext/deriving/mod.rs | 56 ++++++++++----------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index c75a1e1eae1..6fb6dee94ed 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -94,7 +94,7 @@ fn expand_derive(cx: &mut ExtCtxt, } let mut found_partial_eq = false; - let mut found_eq = false; + let mut eq_span = None; for titem in traits.iter().rev() { let tname = match titem.node { @@ -114,12 +114,6 @@ fn expand_derive(cx: &mut ExtCtxt, continue; } - if &tname[..] == "Eq" { - found_eq = true; - } else if &tname[..] == "PartialEq" { - found_partial_eq = true; - } - let span = Span { expn_id: cx.codemap().record_expansion(codemap::ExpnInfo { call_site: titem.span, @@ -131,6 +125,12 @@ fn expand_derive(cx: &mut ExtCtxt, }), ..titem.span }; + if &tname[..] == "Eq" { + eq_span = Some(span); + } else if &tname[..] == "PartialEq" { + found_partial_eq = true; + } + // #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar] item.attrs.push(cx.attribute(span, cx.meta_word(titem.span, intern_and_get_ident(&format!("derive_{}", tname))))); @@ -138,41 +138,13 @@ fn expand_derive(cx: &mut ExtCtxt, // RFC #1445. `#[derive(PartialEq, Eq)]` adds a (trusted) // `#[structural_match]` attribute. - if found_partial_eq && found_eq { - // This span is **very** sensitive and crucial to - // getting the stability behavior we want. What we are - // doing is marking `#[structural_match]` with the - // span of the `#[deriving(...)]` attribute (the - // entire attribute, not just the `PartialEq` or `Eq` - // part), but with the current backtrace. The current - // backtrace will contain a topmost entry that IS this - // `#[deriving(...)]` attribute and with the - // "allow-unstable" flag set to true. - // - // Note that we do NOT use the span of the `Eq` - // text itself. You might think this is - // equivalent, because the `Eq` appears within the - // `#[deriving(Eq)]` attribute, and hence we would - // inherit the "allows unstable" from the - // backtrace. But in fact this is not always the - // case. The actual source text that led to - // deriving can be `#[$attr]`, for example, where - // `$attr == deriving(Eq)`. In that case, the - // "#[structural_match]" would be considered to - // originate not from the deriving call but from - // text outside the deriving call, and hence would - // be forbidden from using unstable - // content. - // - // See tests src/run-pass/rfc1445 for - // examples. --nmatsakis - let span = Span { expn_id: cx.backtrace(), .. span }; - assert!(cx.parse_sess.codemap().span_allows_unstable(span)); - debug!("inserting structural_match with span {:?}", span); - let structural_match = intern_and_get_ident("structural_match"); - item.attrs.push(cx.attribute(span, - cx.meta_word(span, - structural_match))); + if let Some(eq_span) = eq_span { + if found_partial_eq { + let structural_match = intern_and_get_ident("structural_match"); + item.attrs.push(cx.attribute(eq_span, + cx.meta_word(eq_span, + structural_match))); + } } item -- cgit 1.4.1-3-g733a5