make error codes reflect reality better

This commit is contained in:
Jana Dönszelmann
2025-03-08 18:58:05 +01:00
parent 672452d573
commit 5ab5f8a24a
39 changed files with 414 additions and 344 deletions

View File

@@ -1,8 +1,14 @@
#### Note: this error code is no longer emitted by the compiler
This is because it was too specific to the `inline` attribute.
Similar diagnostics occur for other attributes too.
The example here will now emit `E0805`
The `inline` attribute was malformed.
Erroneous code example:
```compile_fail,E0534
```compile_fail,E0805
#[inline()] // error: expected one argument
pub fn something() {}

View File

@@ -1,8 +1,13 @@
An unknown argument was given to the `inline` attribute.
#### Note: this error code is no longer emitted by the compiler
This is because it was too specific to the `inline` attribute.
Similar diagnostics occur for other attributes too.
The example here will now emit `E0539`
Erroneous code example:
```compile_fail,E0535
```compile_fail,E0539
#[inline(unknown)] // error: invalid argument
pub fn something() {}

View File

@@ -24,8 +24,7 @@ struct Stable;
const fn stable_fn() {}
```
Meta items are the key-value pairs inside of an attribute.
To fix these issues you need to give required key-value pairs.
To fix the above example, you can write the following:
```
#![feature(staged_api)]
@@ -49,3 +48,29 @@ struct Stable;
#[rustc_const_stable(feature = "stable_fn", since = "1.39.0")] // ok!
const fn stable_fn() {}
```
Several causes of this are,
an attribute may have expected you to give a list but you gave a
`name = value` pair:
```compile_fail,E0539
// wrong, should be `#[repr(C)]`
#[repr = "C"]
struct Foo {}
```
Or a `name = value` pair, but you gave a list:
```compile_fail,E0539
// wrong, should be `note = "reason"`
#[deprecated(since = "1.0.0", note("reason"))]
struct Foo {}
```
Or it expected some specific word but you gave an unexpected one:
```compile_fail,E0539
// should be `always` or `never`
#[inline(maybe_if_you_feel_like_it)]
fn foo() {}
```

View File

@@ -9,10 +9,9 @@ struct Repr {}
fn main() {}
```
Literals in attributes are new and largely unsupported in built-in attributes.
Work to support literals where appropriate is ongoing. Try using an unquoted
name instead:
Not all attributes support literals in their input,
and in some cases they expect an identifier instead.
That would be the solution in the case of `repr`:
```
#[repr(C)] // ok!
struct Repr {}

View File

@@ -0,0 +1,26 @@
An attribute was given an invalid number of arguments
Erroneous code example:
```compile_fail,E0805
#[inline()] // error! should either have a single argument, or no parentheses
fn foo() {}
#[inline(always, never)] // error! should have only one argument, not two
fn bar() {}
```
To fix this, either give the right number of arguments the attribute needs.
In the case of inline, this could be none at all:
```
#[inline]
fn foo() {}
```
or only one:
```
#[inline(always)]
fn foo() {}
```