Skip to content

Commit 76b4016

Browse files
authored
Mention possibility of linting bad const default values
Ment
1 parent 121a3c0 commit 76b4016

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

text/0000-default-field-values.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -710,32 +710,36 @@ all the following rules apply when type-checking:
710710
2. The expression `def` must coerce to the type `ty`.
711711

712712
3. Generic parameters of the current items are accessible
713-
```rust
714-
struct Bar<const A: usize> {
715-
field: usize = A,
716-
}
717-
```
713+
```rust
714+
struct Bar<const A: usize> {
715+
field: usize = A,
716+
}
717+
```
718718

719719
4. Default const expressions are *not* evaluated at definition time, only
720720
during instantiation. This means that the following will not fail to compile:
721-
```rust
722-
struct Bar {
723-
field1: usize = panic!(),
724-
field2: usize = 42,
725-
}
721+
```rust
722+
struct Bar {
723+
field1: usize = panic!(),
724+
field2: usize = 42,
725+
}
726726

727-
let _ = Bar { field1: 0, .. };
728-
```
727+
let _ = Bar { field1: 0, .. };
728+
```
729+
Having said that, it can be possible to proactivelly attempt to evaluate the
730+
default values and emit a lint in a case where the expression is assured to always
731+
fail (which would only be possible for expressions that do not reference `const`
732+
parameters).
729733

730734
5. The `struct`'s parameters are properly propagated, meaning the following is
731735
possible:
732-
```rust
733-
struct Bar<T> {
734-
field: Vec<T> = Vec::new(),
735-
}
736+
```rust
737+
struct Bar<T> {
738+
field: Vec<T> = Vec::new(),
739+
}
736740

737-
let _ = Bar::<i32> { .. };
738-
```
741+
let _ = Bar::<i32> { .. };
742+
```
739743

740744
When lints check attributes such as `#[allow(lint_name)]` are placed on a
741745
`RecordField`, it also applies to `def` if it exists.

0 commit comments

Comments
 (0)