Skip to content

Commit a5e8db9

Browse files
Fix Cfg add/or operations
1 parent 9fe8aae commit a5e8db9

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,38 @@ pub enum CfgEntry {
197197

198198
impl CfgEntry {
199199
pub fn span(&self) -> Span {
200-
let (CfgEntry::All(_, span)
201-
| CfgEntry::Any(_, span)
202-
| CfgEntry::Not(_, span)
203-
| CfgEntry::Bool(_, span)
204-
| CfgEntry::NameValue { span, .. }
205-
| CfgEntry::Version(_, span)) = self;
200+
let (Self::All(_, span)
201+
| Self::Any(_, span)
202+
| Self::Not(_, span)
203+
| Self::Bool(_, span)
204+
| Self::NameValue { span, .. }
205+
| Self::Version(_, span)) = self;
206206
*span
207207
}
208+
209+
/// Same as `PartialEq` but doesn't check spans and ignore order of cfgs.
210+
pub fn is_equivalent_to(&self, other: &Self) -> bool {
211+
match (self, other) {
212+
(Self::All(a, _), Self::All(b, _)) | (Self::Any(a, _), Self::Any(b, _)) => {
213+
a.len() == b.len() && a.iter().all(|a| b.iter().any(|b| a.is_equivalent_to(b)))
214+
}
215+
(Self::Not(a, _), Self::Not(b, _)) => a.is_equivalent_to(b),
216+
(Self::Bool(a, _), Self::Bool(b, _)) => a == b,
217+
(
218+
Self::NameValue { name: name1, value: value1, .. },
219+
Self::NameValue { name: name2, value: value2, .. },
220+
) => {
221+
name1 == name2
222+
&& match (value1, value2) {
223+
(Some((a, _)), Some((b, _))) => a == b,
224+
(None, None) => true,
225+
_ => false,
226+
}
227+
}
228+
(Self::Version(a, _), Self::Version(b, _)) => a == b,
229+
_ => false,
230+
}
231+
}
208232
}
209233

210234
/// Possible values for the `#[linkage]` attribute, allowing to specify the

src/librustdoc/clean/cfg.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,12 @@ impl Cfg {
280280

281281
fn should_append_only_to_description(&self) -> bool {
282282
match self.0 {
283-
CfgEntry::Bool(..) => false,
284283
CfgEntry::Any(..)
285284
| CfgEntry::All(..)
286285
| CfgEntry::NameValue { .. }
287-
| CfgEntry::Version(..) => true,
288-
CfgEntry::Not(box CfgEntry::NameValue { .. }, _) => true,
289-
CfgEntry::Not(..) => false,
286+
| CfgEntry::Version(..)
287+
| CfgEntry::Not(box CfgEntry::NameValue { .. }, _) => true,
288+
CfgEntry::Not(..) | CfgEntry::Bool(..) => false,
290289
}
291290
}
292291

@@ -347,25 +346,25 @@ impl ops::BitAndAssign for Cfg {
347346
(s @ CfgEntry::Bool(true, _), b) => *s = b,
348347
(CfgEntry::All(a, _), CfgEntry::All(ref mut b, _)) => {
349348
for c in b.drain(..) {
350-
if !a.contains(&c) {
349+
if !a.iter().any(|a| a.is_equivalent_to(&c)) {
351350
a.push(c);
352351
}
353352
}
354353
}
355354
(CfgEntry::All(a, _), ref mut b) => {
356-
if !a.contains(b) {
355+
if !a.iter().any(|a| a.is_equivalent_to(b)) {
357356
a.push(mem::replace(b, CfgEntry::Bool(true, DUMMY_SP)));
358357
}
359358
}
360359
(s, CfgEntry::All(mut a, _)) => {
361360
let b = mem::replace(s, CfgEntry::Bool(true, DUMMY_SP));
362-
if !a.contains(&b) {
361+
if !a.iter().any(|a| a.is_equivalent_to(&b)) {
363362
a.push(b);
364363
}
365364
*s = CfgEntry::All(a, DUMMY_SP);
366365
}
367366
(s, b) => {
368-
if *s != b {
367+
if !s.is_equivalent_to(&b) {
369368
let a = mem::replace(s, CfgEntry::Bool(true, DUMMY_SP));
370369
*s = CfgEntry::All(thin_vec![a, b], DUMMY_SP);
371370
}
@@ -391,25 +390,25 @@ impl ops::BitOrAssign for Cfg {
391390
(s @ CfgEntry::Bool(false, _), b) => *s = b,
392391
(CfgEntry::Any(a, _), CfgEntry::Any(ref mut b, _)) => {
393392
for c in b.drain(..) {
394-
if !a.contains(&c) {
393+
if !a.iter().any(|a| a.is_equivalent_to(&c)) {
395394
a.push(c);
396395
}
397396
}
398397
}
399398
(CfgEntry::Any(a, _), ref mut b) => {
400-
if !a.contains(b) {
399+
if !a.iter().any(|a| a.is_equivalent_to(b)) {
401400
a.push(mem::replace(b, CfgEntry::Bool(true, DUMMY_SP)));
402401
}
403402
}
404403
(s, CfgEntry::Any(mut a, _)) => {
405404
let b = mem::replace(s, CfgEntry::Bool(true, DUMMY_SP));
406-
if !a.contains(&b) {
405+
if !a.iter().any(|a| a.is_equivalent_to(&b)) {
407406
a.push(b);
408407
}
409408
*s = CfgEntry::Any(a, DUMMY_SP);
410409
}
411410
(s, b) => {
412-
if *s != b {
411+
if !s.is_equivalent_to(&b) {
413412
let a = mem::replace(s, CfgEntry::Bool(true, DUMMY_SP));
414413
*s = CfgEntry::Any(thin_vec![a, b], DUMMY_SP);
415414
}
@@ -757,6 +756,7 @@ fn handle_auto_cfg_hide_show(
757756
{
758757
for item in items {
759758
// FIXME: Report in case `Cfg::parse` reports an error?
759+
760760
let Ok(cfg) = Cfg::parse(item) else { continue };
761761
if let CfgEntry::NameValue { name, value, .. } = cfg.0 {
762762
let value = value.map(|(v, _)| v);

0 commit comments

Comments
 (0)