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_NON_ZST_FIELDS,
90
91
RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
91
92
RUST_2021_INCOMPATIBLE_OR_PATTERNS,
@@ -5213,3 +5214,50 @@ declare_lint! {
5213
5214
Warn,
5214
5215
r#"detects when a function annotated with `#[inline(always)]` and `#[target_feature(enable = "..")]` is inlined into a caller without the required target feature"#,
5215
5216
}
5217
+
5218
+
declare_lint!{
5219
+
/// The `repr_c_enums_larger_than_int` lint detects `repr(C)` enums with discriminant
5220
+
/// values that do not fit into a C `int`.
5221
+
///
5222
+
/// ### Example
5223
+
///
5224
+
/// ```rust,ignore (only errors on 64bit)
5225
+
/// #[repr(C)]
5226
+
/// enum E {
5227
+
/// V = 9223372036854775807, // i64::MAX
5228
+
/// }
5229
+
/// ```
5230
+
///
5231
+
/// This will produce:
5232
+
///
5233
+
/// ```text
5234
+
/// error: `repr(C)` enum discriminant does not fit into C `int`
5235
+
/// --> $DIR/repr-c-big-discriminant1.rs:16:5
5236
+
/// |
5237
+
/// LL | A = 9223372036854775807, // i64::MAX
5238
+
/// | ^
5239
+
/// |
5240
+
/// = note: `repr(C)` enums with big discriminants are non-portable, and their size in Rust might not match their size in C
5241
+
/// = help: use `repr($int_ty)` instead to explicitly set the size of this enum
5242
+
/// ```
5243
+
///
5244
+
/// ### Explanation
5245
+
///
5246
+
/// In C, enums with discriminants that do not fit into an `int` are a portability hazard: such
5247
+
/// enums are only permitted since C23, and not supported e.g. by MSVC. Furthermore, Rust
5248
+
/// interprets the discriminant values of `repr(C)` enums as expressions of type `isize`, which
5249
+
/// cannot be changed in a backwards-compatible way. If the discriminant is given as a literal
5250
+
/// that does not fit into `isize`, it is wrapped (with a warning). This makes it impossible to
5251
+
/// implement the C23 behavior of enums where the enum discriminants have no predefined type and
5252
+
/// instead the enum uses a type large enough to hold all discriminants.
5253
+
///
5254
+
/// Therefore, `repr(C)` enums require all discriminants to fit into a C `int`.
5255
+
pubREPR_C_ENUMS_LARGER_THAN_INT,
5256
+
Warn,
5257
+
"repr(C) enums with discriminant values that do not fit into a C int",
0 commit comments