Skip to content

Commit dc6afd7

Browse files
Rollup merge of #150883 - improve-deprecated-intra-doc-span, r=camelid
Improve span for "unresolved intra doc link" on `deprecated` attribute Follow-up of #150721. To make this work, I replaced the `Symbol` by an `Ident` to keep the `Span` information. cc @folkertdev r? @camelid
2 parents 002b68d + f9c71df commit dc6afd7

9 files changed

Lines changed: 42 additions & 29 deletions

File tree

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,14 @@ impl AttributeExt for Attribute {
235235
}
236236
}
237237

238-
fn deprecation_note(&self) -> Option<Symbol> {
238+
fn deprecation_note(&self) -> Option<Ident> {
239239
match &self.kind {
240240
AttrKind::Normal(normal) if normal.item.path == sym::deprecated => {
241241
let meta = &normal.item;
242242

243243
// #[deprecated = "..."]
244244
if let Some(s) = meta.value_str() {
245-
return Some(s);
245+
return Some(Ident { name: s, span: meta.span() });
246246
}
247247

248248
// #[deprecated(note = "...")]
@@ -252,7 +252,7 @@ impl AttributeExt for Attribute {
252252
&& mi.path == sym::note
253253
&& let Some(s) = mi.value_str()
254254
{
255-
return Some(s);
255+
return Some(Ident { name: s, span: mi.span });
256256
}
257257
}
258258
}
@@ -905,7 +905,7 @@ pub trait AttributeExt: Debug {
905905
/// Returns the deprecation note if this is deprecation attribute.
906906
/// * `#[deprecated = "note"]` returns `Some("note")`.
907907
/// * `#[deprecated(note = "note", ...)]` returns `Some("note")`.
908-
fn deprecation_note(&self) -> Option<Symbol>;
908+
fn deprecation_note(&self) -> Option<Ident>;
909909

910910
fn is_proc_macro_attr(&self) -> bool {
911911
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ fn get<S: Stage>(
1313
name: Symbol,
1414
param_span: Span,
1515
arg: &ArgParser,
16-
item: &Option<Symbol>,
17-
) -> Option<Symbol> {
16+
item: Option<Symbol>,
17+
) -> Option<Ident> {
1818
if item.is_some() {
1919
cx.duplicate_key(param_span, name);
2020
return None;
2121
}
2222
if let Some(v) = arg.name_value() {
23-
if let Some(value_str) = v.value_as_str() {
23+
if let Some(value_str) = v.value_as_ident() {
2424
Some(value_str)
2525
} else {
2626
cx.expected_string_literal(v.value_span, Some(&v.value_as_lit()));
@@ -72,7 +72,7 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
7272
let features = cx.features();
7373

7474
let mut since = None;
75-
let mut note = None;
75+
let mut note: Option<Ident> = None;
7676
let mut suggestion = None;
7777

7878
let is_rustc = features.staged_api();
@@ -92,10 +92,16 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
9292

9393
match ident_name {
9494
Some(name @ sym::since) => {
95-
since = Some(get(cx, name, param.span(), param.args(), &since)?);
95+
since = Some(get(cx, name, param.span(), param.args(), since)?.name);
9696
}
9797
Some(name @ sym::note) => {
98-
note = Some(get(cx, name, param.span(), param.args(), &note)?);
98+
note = Some(get(
99+
cx,
100+
name,
101+
param.span(),
102+
param.args(),
103+
note.map(|ident| ident.name),
104+
)?);
99105
}
100106
Some(name @ sym::suggestion) => {
101107
if !features.deprecated_suggestion() {
@@ -107,7 +113,7 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
107113
}
108114

109115
suggestion =
110-
Some(get(cx, name, param.span(), param.args(), &suggestion)?);
116+
Some(get(cx, name, param.span(), param.args(), suggestion)?.name);
111117
}
112118
_ => {
113119
cx.expected_specific_argument(
@@ -124,7 +130,7 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
124130
}
125131
}
126132
ArgParser::NameValue(v) => {
127-
let Some(value) = v.value_as_str() else {
133+
let Some(value) = v.value_as_ident() else {
128134
cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
129135
return None;
130136
};

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ impl NameValueParser {
322322
self.value_as_lit().kind.str()
323323
}
324324

325+
/// If the value is a string literal, it will return its value associated with its span (an
326+
/// `Ident` in short).
327+
pub fn value_as_ident(&self) -> Option<Ident> {
328+
let meta_item = self.value_as_lit();
329+
meta_item.kind.str().map(|name| Ident { name, span: meta_item.span })
330+
}
331+
325332
pub fn args_span(&self) -> Span {
326333
self.eq_span.to(self.value_span)
327334
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub enum IntType {
136136
pub struct Deprecation {
137137
pub since: DeprecatedSince,
138138
/// The note to issue a reason.
139-
pub note: Option<Symbol>,
139+
pub note: Option<Ident>,
140140
/// A text snippet used to completely replace any use of the deprecated item in an expression.
141141
///
142142
/// This is currently unstable.

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ impl AttributeExt for Attribute {
14101410
}
14111411

14121412
#[inline]
1413-
fn deprecation_note(&self) -> Option<Symbol> {
1413+
fn deprecation_note(&self) -> Option<Ident> {
14141414
match &self {
14151415
Attribute::Parsed(AttributeKind::Deprecation { deprecation, .. }) => deprecation.note,
14161416
_ => None,

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub fn early_report_macro_deprecation(
185185
let diag = BuiltinLintDiag::DeprecatedMacro {
186186
suggestion: depr.suggestion,
187187
suggestion_span: span,
188-
note: depr.note,
188+
note: depr.note.map(|ident| ident.name),
189189
path,
190190
since_kind: deprecated_since_kind(is_in_effect, depr.since),
191191
};
@@ -228,7 +228,7 @@ fn late_report_deprecation(
228228
}),
229229
kind: def_kind.to_owned(),
230230
path: def_path,
231-
note: depr.note,
231+
note: depr.note.map(|ident| ident.name),
232232
since_kind: deprecated_since_kind(is_in_effect, depr.since),
233233
};
234234
tcx.emit_node_span_lint(lint, hir_id, method_span, diag);

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_resolve::rustdoc::{
2626
use rustc_session::Session;
2727
use rustc_span::hygiene::MacroKind;
2828
use rustc_span::symbol::{Symbol, kw, sym};
29-
use rustc_span::{DUMMY_SP, FileName, Loc, RemapPathScopeComponents};
29+
use rustc_span::{DUMMY_SP, FileName, Ident, Loc, RemapPathScopeComponents};
3030
use tracing::{debug, trace};
3131
use {rustc_ast as ast, rustc_hir as hir};
3232

@@ -418,7 +418,7 @@ impl Item {
418418
{
419419
Some(Deprecation {
420420
since: DeprecatedSince::Unspecified,
421-
note: Some(note),
421+
note: Some(Ident { name: note, span: DUMMY_SP }),
422422
suggestion: None,
423423
})
424424
} else {
@@ -455,7 +455,7 @@ impl Item {
455455
.attrs
456456
.other_attrs
457457
.iter()
458-
.filter_map(|attr| attr.deprecation_note().map(|_| attr.span()));
458+
.filter_map(|attr| attr.deprecation_note().map(|note| note.span));
459459

460460
span_of_fragments(&self.attrs.doc_strings)
461461
.into_iter()

tests/rustdoc-ui/intra-doc/deprecated.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unresolved link to `TypeAlias::hoge`
2-
--> $DIR/deprecated.rs:3:1
2+
--> $DIR/deprecated.rs:3:16
33
|
44
LL | #[deprecated = "[broken cross-reference](TypeAlias::hoge)"]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: the link appears in this line:
88

@@ -16,10 +16,10 @@ LL | #![deny(rustdoc::broken_intra_doc_links)]
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

1818
error: unresolved link to `TypeAlias::hoge`
19-
--> $DIR/deprecated.rs:6:1
19+
--> $DIR/deprecated.rs:6:38
2020
|
2121
LL | #[deprecated(since = "0.0.0", note = "[broken cross-reference](TypeAlias::hoge)")]
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= note: the link appears in this line:
2525

@@ -28,10 +28,10 @@ LL | #[deprecated(since = "0.0.0", note = "[broken cross-reference](TypeAlias::h
2828
= note: no item named `TypeAlias` in scope
2929

3030
error: unresolved link to `TypeAlias::hoge`
31-
--> $DIR/deprecated.rs:9:1
31+
--> $DIR/deprecated.rs:9:21
3232
|
3333
LL | #[deprecated(note = "[broken cross-reference](TypeAlias::hoge)", since = "0.0.0")]
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3535
|
3636
= note: the link appears in this line:
3737

tests/ui/unpretty/deprecated-attr.stdout

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ use ::std::prelude::rust_2015::*;
1010
struct PlainDeprecated;
1111

1212
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified,
13-
note: "here's why this is deprecated"}}]
13+
note: here's why this is deprecated#0}}]
1414
struct DirectNote;
1515

1616
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified,
17-
note: "here's why this is deprecated"}}]
17+
note: here's why this is deprecated#0}}]
1818
struct ExplicitNote;
1919

2020
#[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
21-
note: "here's why this is deprecated"}}]
21+
note: here's why this is deprecated#0}}]
2222
struct SinceAndNote;
2323

2424
#[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
25-
note: "here's why this is deprecated"}}]
25+
note: here's why this is deprecated#0}}]
2626
struct FlippedOrder;
2727

2828
fn f() {

0 commit comments

Comments
 (0)