Rollup merge of #108000 - y21:no-zero-init-for-uninhabited, r=jackh726
lint: don't suggest MaybeUninit::assume_init for uninhabited types Creating a zeroed uninhabited type such as `!` or an empty enum with `mem::zeroed()` (or transmuting `()` to `!`) currently triggers this lint: ```rs warning: the type `!` does not permit zero-initialization --> test.rs:5:23 | 5 | let _val: ! = mem::zeroed(); | ^^^^^^^^^^^^^ | | | this code causes undefined behavior when executed | help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done | = note: the `!` type has no valid value ``` The `MaybeUninit` suggestion in the help message seems confusing/useless for uninhabited types, as such a type cannot be fully initialized in the first place (as the note implies). This PR limits this help message to inhabited types which can be initialized
This commit is contained in:
commit
4dea3a295f
6 changed files with 32 additions and 59 deletions
|
@ -2635,7 +2635,13 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
|
|||
cx.emit_spanned_lint(
|
||||
INVALID_VALUE,
|
||||
expr.span,
|
||||
BuiltinUnpermittedTypeInit { msg, ty: conjured_ty, label: expr.span, sub },
|
||||
BuiltinUnpermittedTypeInit {
|
||||
msg,
|
||||
ty: conjured_ty,
|
||||
label: expr.span,
|
||||
sub,
|
||||
tcx: cx.tcx,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ use rustc_errors::{
|
|||
};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_macros::{LintDiagnostic, Subdiagnostic};
|
||||
use rustc_middle::ty::{PolyExistentialTraitRef, Predicate, Ty, TyCtxt};
|
||||
use rustc_middle::ty::{
|
||||
inhabitedness::InhabitedPredicate, PolyExistentialTraitRef, Predicate, Ty, TyCtxt,
|
||||
};
|
||||
use rustc_session::parse::ParseSess;
|
||||
use rustc_span::{edition::Edition, sym, symbol::Ident, Span, Symbol};
|
||||
|
||||
|
@ -419,6 +421,7 @@ pub struct BuiltinUnpermittedTypeInit<'a> {
|
|||
pub ty: Ty<'a>,
|
||||
pub label: Span,
|
||||
pub sub: BuiltinUnpermittedTypeInitSub,
|
||||
pub tcx: TyCtxt<'a>,
|
||||
}
|
||||
|
||||
impl<'a> DecorateLint<'a, ()> for BuiltinUnpermittedTypeInit<'_> {
|
||||
|
@ -428,7 +431,13 @@ impl<'a> DecorateLint<'a, ()> for BuiltinUnpermittedTypeInit<'_> {
|
|||
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
|
||||
diag.set_arg("ty", self.ty);
|
||||
diag.span_label(self.label, fluent::lint_builtin_unpermitted_type_init_label);
|
||||
diag.span_label(self.label, fluent::lint_builtin_unpermitted_type_init_label_suggestion);
|
||||
if let InhabitedPredicate::True = self.ty.inhabited_predicate(self.tcx) {
|
||||
// Only suggest late `MaybeUninit::assume_init` initialization if the type is inhabited.
|
||||
diag.span_label(
|
||||
self.label,
|
||||
fluent::lint_builtin_unpermitted_type_init_label_suggestion,
|
||||
);
|
||||
}
|
||||
self.sub.add_to_diagnostic(diag);
|
||||
diag
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue