Skip to content

Commit 6f83263

Browse files
Improve spans for auto_cfg(hide/show) errors
1 parent a71f60c commit 6f83263

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ pub struct CfgInfo {
504504
pub value: Option<(Symbol, Span)>,
505505
}
506506

507+
impl CfgInfo {
508+
pub fn span_for_name_and_value(&self) -> Span {
509+
if let Some((_, value_span)) = self.value {
510+
self.name_span.with_hi(value_span.hi())
511+
} else {
512+
self.name_span
513+
}
514+
}
515+
}
516+
507517
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
508518
pub struct CfgHideShow {
509519
pub kind: HideOrShow,

src/librustdoc/clean/cfg.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn is_simple_cfg(cfg: &CfgEntry) -> bool {
4747
}
4848
}
4949

50-
/// Whether the configuration consists of just `Cfg`, `Not` or `All`.
50+
/// Returns `false` if is `Any`, otherwise returns `true`.
5151
fn is_all_cfg(cfg: &CfgEntry) -> bool {
5252
match cfg {
5353
CfgEntry::Bool(..)
@@ -756,24 +756,23 @@ fn handle_auto_cfg_hide_show(
756756
tcx: TyCtxt<'_>,
757757
cfg_info: &mut CfgInfo,
758758
attr: &CfgHideShow,
759-
attr_span: Span,
760759
new_show_attrs: &mut FxHashMap<(Symbol, Option<Symbol>), rustc_span::Span>,
761760
new_hide_attrs: &mut FxHashMap<(Symbol, Option<Symbol>), rustc_span::Span>,
762761
) {
763762
for value in &attr.values {
764763
let simple = NameValueCfg::from(value);
765764
if attr.kind == HideOrShow::Show {
766765
if let Some(span) = new_hide_attrs.get(&(simple.name, simple.value)) {
767-
show_hide_show_conflict_error(tcx, attr_span, *span);
766+
show_hide_show_conflict_error(tcx, value.span_for_name_and_value(), *span);
768767
} else {
769-
new_show_attrs.insert((simple.name, simple.value), attr_span);
768+
new_show_attrs.insert((simple.name, simple.value), value.span_for_name_and_value());
770769
}
771770
cfg_info.hidden_cfg.remove(&simple);
772771
} else {
773772
if let Some(span) = new_show_attrs.get(&(simple.name, simple.value)) {
774-
show_hide_show_conflict_error(tcx, attr_span, *span);
773+
show_hide_show_conflict_error(tcx, value.span_for_name_and_value(), *span);
775774
} else {
776-
new_hide_attrs.insert((simple.name, simple.value), attr_span);
775+
new_hide_attrs.insert((simple.name, simple.value), value.span_for_name_and_value());
777776
}
778777
cfg_info.hidden_cfg.insert(simple);
779778
}
@@ -871,12 +870,11 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
871870
) {
872871
return None;
873872
}
874-
for (value, span) in &d.auto_cfg {
873+
for (value, _) in &d.auto_cfg {
875874
handle_auto_cfg_hide_show(
876875
tcx,
877876
cfg_info,
878877
value,
879-
*span,
880878
&mut new_show_attrs,
881879
&mut new_hide_attrs,
882880
);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error: same `cfg` was in `auto_cfg(hide(...))` and `auto_cfg(show(...))` on the same item
2-
--> $DIR/cfg-hide-show-conflict.rs:3:8
2+
--> $DIR/cfg-hide-show-conflict.rs:3:31
33
|
44
LL | #![doc(auto_cfg(show(windows, target_os = "linux")))]
5-
| ^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
note: first change was here
8-
--> $DIR/cfg-hide-show-conflict.rs:2:8
8+
--> $DIR/cfg-hide-show-conflict.rs:2:22
99
|
1010
LL | #![doc(auto_cfg(hide(target_os = "linux")))]
11-
| ^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^
1212

1313
error: aborting due to 1 previous error
1414

0 commit comments

Comments
 (0)