Improve spans for auto_cfg(hide/show) errors

This commit is contained in:
Guillaume Gomez 2025-12-08 18:06:36 +01:00
parent 1da7684c7d
commit 4191e94715
3 changed files with 20 additions and 12 deletions

View file

@ -504,6 +504,16 @@ pub struct CfgInfo {
pub value: Option<(Symbol, Span)>,
}
impl CfgInfo {
pub fn span_for_name_and_value(&self) -> Span {
if let Some((_, value_span)) = self.value {
self.name_span.with_hi(value_span.hi())
} else {
self.name_span
}
}
}
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
pub struct CfgHideShow {
pub kind: HideOrShow,

View file

@ -47,7 +47,7 @@ fn is_simple_cfg(cfg: &CfgEntry) -> bool {
}
}
/// Whether the configuration consists of just `Cfg`, `Not` or `All`.
/// Returns `false` if is `Any`, otherwise returns `true`.
fn is_all_cfg(cfg: &CfgEntry) -> bool {
match cfg {
CfgEntry::Bool(..)
@ -756,7 +756,6 @@ fn handle_auto_cfg_hide_show(
tcx: TyCtxt<'_>,
cfg_info: &mut CfgInfo,
attr: &CfgHideShow,
attr_span: Span,
new_show_attrs: &mut FxHashMap<(Symbol, Option<Symbol>), rustc_span::Span>,
new_hide_attrs: &mut FxHashMap<(Symbol, Option<Symbol>), rustc_span::Span>,
) {
@ -764,16 +763,16 @@ fn handle_auto_cfg_hide_show(
let simple = NameValueCfg::from(value);
if attr.kind == HideOrShow::Show {
if let Some(span) = new_hide_attrs.get(&(simple.name, simple.value)) {
show_hide_show_conflict_error(tcx, attr_span, *span);
show_hide_show_conflict_error(tcx, value.span_for_name_and_value(), *span);
} else {
new_show_attrs.insert((simple.name, simple.value), attr_span);
new_show_attrs.insert((simple.name, simple.value), value.span_for_name_and_value());
}
cfg_info.hidden_cfg.remove(&simple);
} else {
if let Some(span) = new_show_attrs.get(&(simple.name, simple.value)) {
show_hide_show_conflict_error(tcx, attr_span, *span);
show_hide_show_conflict_error(tcx, value.span_for_name_and_value(), *span);
} else {
new_hide_attrs.insert((simple.name, simple.value), attr_span);
new_hide_attrs.insert((simple.name, simple.value), value.span_for_name_and_value());
}
cfg_info.hidden_cfg.insert(simple);
}
@ -871,12 +870,11 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
) {
return None;
}
for (value, span) in &d.auto_cfg {
for (value, _) in &d.auto_cfg {
handle_auto_cfg_hide_show(
tcx,
cfg_info,
value,
*span,
&mut new_show_attrs,
&mut new_hide_attrs,
);

View file

@ -1,14 +1,14 @@
error: same `cfg` was in `auto_cfg(hide(...))` and `auto_cfg(show(...))` on the same item
--> $DIR/cfg-hide-show-conflict.rs:3:8
--> $DIR/cfg-hide-show-conflict.rs:3:31
|
LL | #![doc(auto_cfg(show(windows, target_os = "linux")))]
| ^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^
|
note: first change was here
--> $DIR/cfg-hide-show-conflict.rs:2:8
--> $DIR/cfg-hide-show-conflict.rs:2:22
|
LL | #![doc(auto_cfg(hide(target_os = "linux")))]
| ^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error