Skip to content

Commit 0dcccb7

Browse files
committed
Migrate more
1 parent b177323 commit 0dcccb7

File tree

7 files changed

+136
-181
lines changed

7 files changed

+136
-181
lines changed

compiler/rustc_error_messages/locales/en-US/rustdoc.ftl

+19
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,22 @@ rustdoc_error_reading_file_not_utf8 =
109109
110110
rustdoc_error_loading_examples =
111111
failed to load examples: {$error} (for path {$path})
112+
113+
rustdoc_anonymous_imports_cannot_be_inlined =
114+
anonymous imports cannot be inlined
115+
.import_span = anonymous import
116+
117+
rustdoc_invalid_codeblock_attribute =
118+
unknown attribute `{$attr_name}`. Did you mean `{$suggested_attr_name}`?
119+
.compile_fail = the code block will either not be tested if not marked as a rust one or won't fail if it compiles successfully
120+
.should_panic = the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running
121+
.no_run = the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
122+
.test_harness = the code block will either not be tested if not marked as a rust one or the code will be wrapped inside a main function
123+
124+
rustdoc_failed_to_read_file =
125+
failed to read file {$path}: {$error}
126+
127+
rustdoc_bare_url_not_hyperlink =
128+
this URL is not a hyperlink
129+
.note = bare URLs are not automatically turned into clickable links
130+
.suggestion = use an automatic link instead

src/librustdoc/clean/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use std::mem;
3838
use thin_vec::ThinVec;
3939

4040
use crate::core::{self, DocContext, ImplTraitParam};
41+
use crate::errors::AnonymousImportsCannotBeInlined;
4142
use crate::formats::item_type::ItemType;
4243
use crate::visit_ast::Module as DocModule;
4344

@@ -2405,14 +2406,10 @@ fn clean_use_statement_inner<'tcx>(
24052406

24062407
if pub_underscore {
24072408
if let Some(ref inline) = inline_attr {
2408-
rustc_errors::struct_span_err!(
2409-
cx.tcx.sess,
2410-
inline.span(),
2411-
E0780,
2412-
"anonymous imports cannot be inlined"
2413-
)
2414-
.span_label(import.span, "anonymous import")
2415-
.emit();
2409+
cx.tcx.sess.emit_err(AnonymousImportsCannotBeInlined {
2410+
inline_span: inline.span(),
2411+
import_span: import.span,
2412+
});
24162413
}
24172414
}
24182415

src/librustdoc/errors.rs

+69
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::{DecorateLint, DiagnosticMessage};
12
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
23
use rustc_span::Span;
34

@@ -179,3 +180,71 @@ pub struct ErrorLoadingExamples {
179180
pub error: io::Error,
180181
pub path: String,
181182
}
183+
184+
#[derive(Diagnostic)]
185+
#[diag(rustdoc_anonymous_imports_cannot_be_inlined, code = "E0780")]
186+
pub struct AnonymousImportsCannotBeInlined {
187+
#[primary_span]
188+
pub inline_span: Span,
189+
#[label(import_span)]
190+
pub import_span: Span,
191+
}
192+
193+
pub struct InvalidCodeblockAttribute<'a> {
194+
pub attr_name: &'a str,
195+
pub suggested_attr_kind: CodeblockAttributeKind,
196+
}
197+
198+
impl<'a> DecorateLint<'a, ()> for InvalidCodeblockAttribute<'_> {
199+
fn decorate_lint<'b>(
200+
self,
201+
diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>,
202+
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
203+
use CodeblockAttributeKind::*;
204+
205+
diag.set_arg("attr_name", self.attr_name);
206+
let suggested_attr_name = match self.suggested_attr_kind {
207+
CompileFail => "compile_fail",
208+
ShouldPanic => "should_panic",
209+
NoRun => "no_run",
210+
TestHarness => "test_harness",
211+
};
212+
diag.set_arg("suggested_attr_name", suggested_attr_name);
213+
diag.subdiagnostic(self.suggested_attr_kind);
214+
diag
215+
}
216+
217+
fn msg(&self) -> DiagnosticMessage {
218+
rustc_errors::fluent::rustdoc_invalid_codeblock_attribute
219+
}
220+
}
221+
222+
#[derive(Subdiagnostic)]
223+
pub enum CodeblockAttributeKind {
224+
#[help(compile_fail)]
225+
CompileFail,
226+
#[help(should_panic)]
227+
ShouldPanic,
228+
#[help(no_run)]
229+
NoRun,
230+
#[help(test_harness)]
231+
TestHarness,
232+
}
233+
234+
#[derive(Diagnostic)]
235+
#[diag(rustdoc_failed_to_read_file)]
236+
pub struct FailedToReadFile<'a> {
237+
#[primary_span]
238+
pub span: Span,
239+
pub path: &'a Path,
240+
pub error: io::Error,
241+
}
242+
243+
#[derive(LintDiagnostic)]
244+
#[note]
245+
#[diag(rustdoc_bare_url_not_hyperlink)]
246+
pub struct BareUrlNotHyperlink<'a> {
247+
#[suggestion(code = "<{url}>", applicability = "machine-applicable")]
248+
pub span: Span,
249+
pub url: &'a str,
250+
}

src/librustdoc/html/markdown.rs

+17-37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::str;
4343

4444
use crate::clean::RenderedLink;
4545
use crate::doctest;
46+
use crate::errors::{CodeblockAttributeKind, InvalidCodeblockAttribute};
4647
use crate::html::escape::Escape;
4748
use crate::html::format::Buffer;
4849
use crate::html::highlight;
@@ -803,7 +804,7 @@ impl<'tcx> ExtraInfo<'tcx> {
803804
ExtraInfo { id: ExtraInfoId::Def(did), sp, tcx }
804805
}
805806

806-
fn error_invalid_codeblock_attr(&self, msg: &str, help: &str) {
807+
fn error_invalid_codeblock_attr(&self, name: &str, suggested_kind: CodeblockAttributeKind) {
807808
let hir_id = match self.id {
808809
ExtraInfoId::Hir(hir_id) => hir_id,
809810
ExtraInfoId::Def(item_did) => {
@@ -816,12 +817,11 @@ impl<'tcx> ExtraInfo<'tcx> {
816817
}
817818
}
818819
};
819-
self.tcx.struct_span_lint_hir(
820+
self.tcx.emit_spanned_lint(
820821
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
821822
hir_id,
822823
self.sp,
823-
msg,
824-
|lint| lint.help(help),
824+
InvalidCodeblockAttribute { attr_name: name, suggested_attr_kind: suggested_kind },
825825
);
826826
}
827827
}
@@ -956,41 +956,21 @@ impl LangString {
956956
}
957957
x if extra.is_some() => {
958958
let s = x.to_lowercase();
959-
if let Some((flag, help)) = if s == "compile-fail"
960-
|| s == "compile_fail"
961-
|| s == "compilefail"
959+
if let Some(kind) =
960+
if s == "compile-fail" || s == "compile_fail" || s == "compilefail" {
961+
Some(CodeblockAttributeKind::CompileFail)
962+
} else if s == "should-panic" || s == "should_panic" || s == "shouldpanic" {
963+
Some(CodeblockAttributeKind::ShouldPanic)
964+
} else if s == "no-run" || s == "no_run" || s == "norun" {
965+
Some(CodeblockAttributeKind::NoRun)
966+
} else if s == "test-harness" || s == "test_harness" || s == "testharness" {
967+
Some(CodeblockAttributeKind::TestHarness)
968+
} else {
969+
None
970+
}
962971
{
963-
Some((
964-
"compile_fail",
965-
"the code block will either not be tested if not marked as a rust one \
966-
or won't fail if it compiles successfully",
967-
))
968-
} else if s == "should-panic" || s == "should_panic" || s == "shouldpanic" {
969-
Some((
970-
"should_panic",
971-
"the code block will either not be tested if not marked as a rust one \
972-
or won't fail if it doesn't panic when running",
973-
))
974-
} else if s == "no-run" || s == "no_run" || s == "norun" {
975-
Some((
976-
"no_run",
977-
"the code block will either not be tested if not marked as a rust one \
978-
or will be run (which you might not want)",
979-
))
980-
} else if s == "test-harness" || s == "test_harness" || s == "testharness" {
981-
Some((
982-
"test_harness",
983-
"the code block will either not be tested if not marked as a rust one \
984-
or the code will be wrapped inside a main function",
985-
))
986-
} else {
987-
None
988-
} {
989972
if let Some(extra) = extra {
990-
extra.error_invalid_codeblock_attr(
991-
&format!("unknown attribute `{}`. Did you mean `{}`?", x, flag),
992-
help,
993-
);
973+
extra.error_invalid_codeblock_attr(x, kind);
994974
}
995975
}
996976
seen_other_tags = true;

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use serde::{Serialize, Serializer};
6464

6565
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
6666
use crate::error::Error;
67+
use crate::errors::FailedToReadFile;
6768
use crate::formats::cache::Cache;
6869
use crate::formats::item_type::ItemType;
6970
use crate::formats::{AssocItemRender, Impl, RenderMode};
@@ -2854,10 +2855,9 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
28542855
let write_example = |w: &mut Buffer, (path, call_data): (&PathBuf, &CallData)| -> bool {
28552856
let contents = match fs::read_to_string(&path) {
28562857
Ok(contents) => contents,
2857-
Err(err) => {
2858+
Err(error) => {
28582859
let span = item.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner());
2859-
tcx.sess
2860-
.span_err(span, &format!("failed to read file {}: {}", path.display(), err));
2860+
tcx.sess.emit_err(FailedToReadFile { span, path, error });
28612861
return false;
28622862
}
28632863
};

src/librustdoc/passes/lint/bare_urls.rs

+22-36
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
44
use crate::clean::*;
55
use crate::core::DocContext;
6+
use crate::errors::BareUrlNotHyperlink;
67
use crate::html::markdown::main_body_opts;
78
use crate::passes::source_span_for_markdown_range;
89
use core::ops::Range;
910
use pulldown_cmark::{Event, Parser, Tag};
1011
use regex::Regex;
11-
use rustc_errors::Applicability;
1212
use std::mem;
1313
use std::sync::LazyLock;
1414

@@ -20,25 +20,31 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
2020
};
2121
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
2222
if !dox.is_empty() {
23-
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
24-
let sp = source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
25-
.unwrap_or_else(|| item.attr_span(cx.tcx));
26-
cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, msg, |lint| {
27-
lint.note("bare URLs are not automatically turned into clickable links")
28-
.span_suggestion(
29-
sp,
30-
"use an automatic link instead",
31-
format!("<{}>", url),
32-
Applicability::MachineApplicable,
33-
)
34-
});
35-
};
36-
3723
let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
3824

3925
while let Some((event, range)) = p.next() {
4026
match event {
41-
Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
27+
Event::Text(text) => {
28+
trace!("looking for raw urls in {}", text);
29+
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
30+
for match_ in URL_REGEX.find_iter(&text) {
31+
let url = match_.as_str();
32+
let url_range = match_.range();
33+
let range = Range {
34+
start: range.start + url_range.start,
35+
end: range.start + url_range.end,
36+
};
37+
let span =
38+
source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
39+
.unwrap_or_else(|| item.attr_span(cx.tcx));
40+
cx.tcx.emit_spanned_lint(
41+
crate::lint::BARE_URLS,
42+
hir_id,
43+
span,
44+
BareUrlNotHyperlink { span, url },
45+
);
46+
}
47+
}
4248
// We don't want to check the text inside code blocks or links.
4349
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link(..))) => {
4450
while let Some((event, _)) = p.next() {
@@ -67,23 +73,3 @@ static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
6773
))
6874
.expect("failed to build regex")
6975
});
70-
71-
fn find_raw_urls(
72-
cx: &DocContext<'_>,
73-
text: &str,
74-
range: Range<usize>,
75-
f: &impl Fn(&DocContext<'_>, &str, &str, Range<usize>),
76-
) {
77-
trace!("looking for raw urls in {}", text);
78-
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
79-
for match_ in URL_REGEX.find_iter(text) {
80-
let url = match_.as_str();
81-
let url_range = match_.range();
82-
f(
83-
cx,
84-
"this URL is not a hyperlink",
85-
url,
86-
Range { start: range.start + url_range.start, end: range.start + url_range.end },
87-
);
88-
}
89-
}

0 commit comments

Comments
 (0)