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