@@ -94,7 +94,7 @@ fn expand_derive(cx: &mut ExtCtxt,
9494 }
9595
9696 let mut found_partial_eq = false ;
97- let mut found_eq = false ;
97+ let mut eq_span = None ;
9898
9999 for titem in traits. iter ( ) . rev ( ) {
100100 let tname = match titem. node {
@@ -114,12 +114,6 @@ fn expand_derive(cx: &mut ExtCtxt,
114114 continue ;
115115 }
116116
117- if & tname[ ..] == "Eq" {
118- found_eq = true ;
119- } else if & tname[ ..] == "PartialEq" {
120- found_partial_eq = true ;
121- }
122-
123117 let span = Span {
124118 expn_id : cx. codemap ( ) . record_expansion ( codemap:: ExpnInfo {
125119 call_site : titem. span ,
@@ -131,48 +125,26 @@ fn expand_derive(cx: &mut ExtCtxt,
131125 } ) , ..titem. span
132126 } ;
133127
128+ if & tname[ ..] == "Eq" {
129+ eq_span = Some ( span) ;
130+ } else if & tname[ ..] == "PartialEq" {
131+ found_partial_eq = true ;
132+ }
133+
134134 // #[derive(Foo, Bar)] expands to #[derive_Foo] #[derive_Bar]
135135 item. attrs . push ( cx. attribute ( span, cx. meta_word ( titem. span ,
136136 intern_and_get_ident ( & format ! ( "derive_{}" , tname) ) ) ) ) ;
137137 }
138138
139139 // RFC #1445. `#[derive(PartialEq, Eq)]` adds a (trusted)
140140 // `#[structural_match]` attribute.
141- if found_partial_eq && found_eq {
142- // This span is **very** sensitive and crucial to
143- // getting the stability behavior we want. What we are
144- // doing is marking `#[structural_match]` with the
145- // span of the `#[deriving(...)]` attribute (the
146- // entire attribute, not just the `PartialEq` or `Eq`
147- // part), but with the current backtrace. The current
148- // backtrace will contain a topmost entry that IS this
149- // `#[deriving(...)]` attribute and with the
150- // "allow-unstable" flag set to true.
151- //
152- // Note that we do NOT use the span of the `Eq`
153- // text itself. You might think this is
154- // equivalent, because the `Eq` appears within the
155- // `#[deriving(Eq)]` attribute, and hence we would
156- // inherit the "allows unstable" from the
157- // backtrace. But in fact this is not always the
158- // case. The actual source text that led to
159- // deriving can be `#[$attr]`, for example, where
160- // `$attr == deriving(Eq)`. In that case, the
161- // "#[structural_match]" would be considered to
162- // originate not from the deriving call but from
163- // text outside the deriving call, and hence would
164- // be forbidden from using unstable
165- // content.
166- //
167- // See tests src/run-pass/rfc1445 for
168- // examples. --nmatsakis
169- let span = Span { expn_id : cx. backtrace ( ) , .. span } ;
170- assert ! ( cx. parse_sess. codemap( ) . span_allows_unstable( span) ) ;
171- debug ! ( "inserting structural_match with span {:?}" , span) ;
172- let structural_match = intern_and_get_ident ( "structural_match" ) ;
173- item. attrs . push ( cx. attribute ( span,
174- cx. meta_word ( span,
175- structural_match) ) ) ;
141+ if let Some ( eq_span) = eq_span {
142+ if found_partial_eq {
143+ let structural_match = intern_and_get_ident ( "structural_match" ) ;
144+ item. attrs . push ( cx. attribute ( eq_span,
145+ cx. meta_word ( eq_span,
146+ structural_match) ) ) ;
147+ }
176148 }
177149
178150 item
0 commit comments