Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ICE on #![doc(test(...)] with literal parameter #112081

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ passes_doc_keyword_not_mod =
passes_doc_keyword_only_impl =
`#[doc(keyword = "...")]` should be used on impl blocks

passes_doc_test_literal = `#![doc(test(...)]` does not take a literal

passes_doc_test_takes_list =
`#[doc(test(...)]` takes a list of attributes

Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,21 +944,28 @@ impl CheckAttrVisitor<'_> {
let mut is_valid = true;
if let Some(metas) = meta.meta_item_list() {
for i_meta in metas {
match i_meta.name_or_empty() {
sym::attr | sym::no_crate_inject => {}
_ => {
match (i_meta.name_or_empty(), i_meta.meta_item()) {
(sym::attr | sym::no_crate_inject, _) => {}
(_, Some(m)) => {
self.tcx.emit_spanned_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
i_meta.span(),
errors::DocTestUnknown {
path: rustc_ast_pretty::pprust::path_to_string(
&i_meta.meta_item().unwrap().path,
),
path: rustc_ast_pretty::pprust::path_to_string(&m.path),
},
);
is_valid = false;
}
(_, None) => {
self.tcx.emit_spanned_lint(
INVALID_DOC_ATTRIBUTES,
hir_id,
i_meta.span(),
errors::DocTestLiteral,
);
is_valid = false;
}
}
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ pub struct DocTestUnknown {
pub path: String,
}

#[derive(LintDiagnostic)]
#[diag(passes_doc_test_literal)]
pub struct DocTestLiteral;

#[derive(LintDiagnostic)]
#[diag(passes_doc_test_takes_list)]
pub struct DocTestTakesList;
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/attributes/doc-test-literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![deny(warnings)]

#![doc(test(""))]
//~^ ERROR `#![doc(test(...)]` does not take a literal
//~^^ WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/attributes/doc-test-literal.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: `#![doc(test(...)]` does not take a literal
--> $DIR/doc-test-literal.rs:3:13
|
LL | #![doc(test(""))]
| ^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
note: the lint level is defined here
--> $DIR/doc-test-literal.rs:1:9
|
LL | #![deny(warnings)]
| ^^^^^^^^
= note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]`

error: aborting due to previous error