Rollup merge of #91133 - terrarier2111:unsafe-diagnostic, r=jackh726
Improve `unsafe` diagnostic This fixes: https://github.com/rust-lang/rust/issues/90880 I didn't use the exact proposed messages though.
This commit is contained in:
commit
c8133f6f5e
@ -999,10 +999,32 @@ impl<'a> Parser<'a> {
|
|||||||
attrs: &mut Vec<Attribute>,
|
attrs: &mut Vec<Attribute>,
|
||||||
unsafety: Unsafe,
|
unsafety: Unsafe,
|
||||||
) -> PResult<'a, ItemInfo> {
|
) -> PResult<'a, ItemInfo> {
|
||||||
|
let sp_start = self.prev_token.span;
|
||||||
let abi = self.parse_abi(); // ABI?
|
let abi = self.parse_abi(); // ABI?
|
||||||
let items = self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?;
|
match self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No)) {
|
||||||
let module = ast::ForeignMod { unsafety, abi, items };
|
Ok(items) => {
|
||||||
Ok((Ident::empty(), ItemKind::ForeignMod(module)))
|
let module = ast::ForeignMod { unsafety, abi, items };
|
||||||
|
Ok((Ident::empty(), ItemKind::ForeignMod(module)))
|
||||||
|
}
|
||||||
|
Err(mut err) => {
|
||||||
|
let current_qual_sp = self.prev_token.span;
|
||||||
|
let current_qual_sp = current_qual_sp.to(sp_start);
|
||||||
|
if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) {
|
||||||
|
if err.message() == "expected `{`, found keyword `unsafe`" {
|
||||||
|
let invalid_qual_sp = self.token.uninterpolated_span();
|
||||||
|
let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap();
|
||||||
|
|
||||||
|
err.span_suggestion(
|
||||||
|
current_qual_sp.to(invalid_qual_sp),
|
||||||
|
&format!("`{}` must come before `{}`", invalid_qual, current_qual),
|
||||||
|
format!("{} {}", invalid_qual, current_qual),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a foreign item (one in an `extern { ... }` block).
|
/// Parses a foreign item (one in an `extern { ... }` block).
|
||||||
|
@ -4,10 +4,15 @@ error: expected `{`, found keyword `unsafe`
|
|||||||
LL | trait T {
|
LL | trait T {
|
||||||
| - while parsing this item list starting here
|
| - while parsing this item list starting here
|
||||||
LL | extern "Rust" unsafe fn foo();
|
LL | extern "Rust" unsafe fn foo();
|
||||||
| ^^^^^^ expected `{`
|
| --------------^^^^^^
|
||||||
|
| | |
|
||||||
|
| | expected `{`
|
||||||
|
| help: `unsafe` must come before `extern "Rust"`: `unsafe extern "Rust"`
|
||||||
LL |
|
LL |
|
||||||
LL | }
|
LL | }
|
||||||
| - the item list ends here
|
| - the item list ends here
|
||||||
|
|
|
||||||
|
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user