You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: compiler/rustc_lint_defs/src/builtin.rs
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -86,6 +86,7 @@ declare_lint_pass! {
86
86
REFINING_IMPL_TRAIT_INTERNAL,
87
87
REFINING_IMPL_TRAIT_REACHABLE,
88
88
RENAMED_AND_REMOVED_LINTS,
89
+
REPR_C_ENUMS_LARGER_THAN_INT,
89
90
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
90
91
RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
91
92
RUST_2021_INCOMPATIBLE_OR_PATTERNS,
@@ -5200,3 +5201,50 @@ declare_lint! {
5200
5201
Warn,
5201
5202
r#"detects when a function annotated with `#[inline(always)]` and `#[target_feature(enable = "..")]` is inlined into a caller without the required target feature"#,
5202
5203
}
5204
+
5205
+
declare_lint!{
5206
+
/// The `repr_c_enums_larger_than_int` lint detects `repr(C)` enums with discriminant
5207
+
/// values that do not fit into a C `int`.
5208
+
///
5209
+
/// ### Example
5210
+
///
5211
+
/// ```rust,ignore (only errors on 64bit)
5212
+
/// #[repr(C)]
5213
+
/// enum E {
5214
+
/// V = 9223372036854775807, // i64::MAX
5215
+
/// }
5216
+
/// ```
5217
+
///
5218
+
/// This will produce:
5219
+
///
5220
+
/// ```text
5221
+
/// error: `repr(C)` enum discriminant does not fit into C `int`
5222
+
/// --> $DIR/repr-c-big-discriminant1.rs:16:5
5223
+
/// |
5224
+
/// LL | A = 9223372036854775807, // i64::MAX
5225
+
/// | ^
5226
+
/// |
5227
+
/// = note: `repr(C)` enums with big discriminants are non-portable, and their size in Rust might not match their size in C
5228
+
/// = help: use `repr($int_ty)` instead to explicitly set the size of this enum
5229
+
/// ```
5230
+
///
5231
+
/// ### Explanation
5232
+
///
5233
+
/// In C, enums with discriminants that do not fit into an `int` are a portability hazard: such
5234
+
/// enums are only permitted since C23, and not supported e.g. by MSVC. Furthermore, Rust
5235
+
/// interprets the discriminant values of `repr(C)` enums as expressions of type `isize`, which
5236
+
/// cannot be changed in a backwards-compatible way. If the discriminant is given as a literal
5237
+
/// that does not fit into `isize`, it is wrapped (with a warning). This makes it impossible to
5238
+
/// implement the C23 behavior of enums where the enum discriminants have no predefined type and
5239
+
/// instead the enum uses a type large enough to hold all discriminants.
5240
+
///
5241
+
/// Therefore, `repr(C)` enums require all discriminants to fit into a C `int`.
5242
+
pubREPR_C_ENUMS_LARGER_THAN_INT,
5243
+
Warn,
5244
+
"repr(C) enums with discriminant values that do not fit into a C int",
0 commit comments