Rename Unsafe to Safety

This commit is contained in:
Santiago Pastorino 2024-05-17 14:17:48 -03:00
parent 2d89cee625
commit 6b46a919e1
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
115 changed files with 460 additions and 494 deletions

View file

@ -156,7 +156,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
[],
expected_return_type,
false,
hir::Unsafety::Normal,
hir::Safety::Safe,
Abi::Rust,
));
@ -252,7 +252,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
[tcx.types.isize, Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8))],
tcx.types.isize,
false,
hir::Unsafety::Normal,
hir::Safety::Safe,
Abi::Rust,
));

View file

@ -71,13 +71,13 @@ fn equate_intrinsic_type<'tcx>(
}
/// Returns the unsafety of the given intrinsic.
pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Unsafety {
pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Safety {
let has_safe_attr = if tcx.has_attr(intrinsic_id, sym::rustc_intrinsic) {
tcx.fn_sig(intrinsic_id).skip_binder().unsafety()
tcx.fn_sig(intrinsic_id).skip_binder().safety()
} else {
match tcx.has_attr(intrinsic_id, sym::rustc_safe_intrinsic) {
true => hir::Unsafety::Normal,
false => hir::Unsafety::Unsafe,
true => hir::Safety::Safe,
false => hir::Safety::Unsafe,
}
};
let is_in_list = match tcx.item_name(intrinsic_id.into()) {
@ -136,8 +136,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
| sym::fmul_algebraic
| sym::fdiv_algebraic
| sym::frem_algebraic
| sym::const_eval_select => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
| sym::const_eval_select => hir::Safety::Safe,
_ => hir::Safety::Unsafe,
};
if has_safe_attr != is_in_list {
@ -197,7 +197,7 @@ pub fn check_intrinsic_type(
})
};
let (n_tps, n_lts, n_cts, inputs, output, unsafety) = if name_str.starts_with("atomic_") {
let (n_tps, n_lts, n_cts, inputs, output, safety) = if name_str.starts_with("atomic_") {
let split: Vec<&str> = name_str.split('_').collect();
assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
@ -219,9 +219,9 @@ pub fn check_intrinsic_type(
return;
}
};
(n_tps, 0, 0, inputs, output, hir::Unsafety::Unsafe)
(n_tps, 0, 0, inputs, output, hir::Safety::Unsafe)
} else {
let unsafety = intrinsic_operation_unsafety(tcx, intrinsic_id);
let safety = intrinsic_operation_unsafety(tcx, intrinsic_id);
let (n_tps, n_cts, inputs, output) = match intrinsic_name {
sym::abort => (0, 0, vec![], tcx.types.never),
sym::unreachable => (0, 0, vec![], tcx.types.never),
@ -514,14 +514,14 @@ pub fn check_intrinsic_type(
[mut_u8],
tcx.types.unit,
false,
hir::Unsafety::Normal,
hir::Safety::Safe,
Abi::Rust,
));
let catch_fn_ty = ty::Binder::dummy(tcx.mk_fn_sig(
[mut_u8, mut_u8],
tcx.types.unit,
false,
hir::Unsafety::Normal,
hir::Safety::Safe,
Abi::Rust,
));
(
@ -656,9 +656,9 @@ pub fn check_intrinsic_type(
return;
}
};
(n_tps, 0, n_cts, inputs, output, unsafety)
(n_tps, 0, n_cts, inputs, output, safety)
};
let sig = tcx.mk_fn_sig(inputs, output, false, unsafety, abi);
let sig = tcx.mk_fn_sig(inputs, output, false, safety, abi);
let sig = ty::Binder::bind_with_vars(sig, bound_vars);
equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, n_lts, n_cts, sig)
}

View file

@ -456,7 +456,7 @@ fn fn_sig_suggestion<'tcx>(
let output = if !output.is_unit() { format!(" -> {output}") } else { String::new() };
let unsafety = sig.unsafety.prefix_str();
let safety = sig.safety.prefix_str();
let (generics, where_clauses) = bounds_from_generic_predicates(tcx, predicates);
// FIXME: this is not entirely correct, as the lifetimes from borrowed params will
@ -464,9 +464,7 @@ fn fn_sig_suggestion<'tcx>(
// lifetimes between the `impl` and the `trait`, but this should be good enough to
// fill in a significant portion of the missing code, and other subsequent
// suggestions can help the user fix the code.
format!(
"{unsafety}{asyncness}fn {ident}{generics}({args}){output}{where_clauses} {{ todo!() }}"
)
format!("{safety}{asyncness}fn {ident}{generics}({args}){output}{where_clauses} {{ todo!() }}")
}
/// Return placeholder code for the given associated item.

View file

@ -2,7 +2,7 @@
//! crate or pertains to a type defined in this crate.
use rustc_errors::{codes::*, struct_span_code_err};
use rustc_hir::Unsafety;
use rustc_hir::Safety;
use rustc_middle::ty::print::PrintTraitRefExt as _;
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TraitDef, TyCtxt};
use rustc_span::def_id::LocalDefId;
@ -18,8 +18,8 @@ pub(super) fn check_item(
tcx.generics_of(def_id).own_params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
let trait_ref = trait_header.trait_ref.instantiate_identity();
match (trait_def.unsafety, unsafe_attr, trait_header.unsafety, trait_header.polarity) {
(Unsafety::Normal, None, Unsafety::Unsafe, Positive | Reservation) => {
match (trait_def.safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
(Safety::Safe, None, Safety::Unsafe, Positive | Reservation) => {
let span = tcx.def_span(def_id);
return Err(struct_span_code_err!(
tcx.dcx(),
@ -37,7 +37,7 @@ pub(super) fn check_item(
.emit());
}
(Unsafety::Unsafe, _, Unsafety::Normal, Positive | Reservation) => {
(Safety::Unsafe, _, Safety::Safe, Positive | Reservation) => {
let span = tcx.def_span(def_id);
return Err(struct_span_code_err!(
tcx.dcx(),
@ -61,7 +61,7 @@ pub(super) fn check_item(
.emit());
}
(Unsafety::Normal, Some(attr_name), Unsafety::Normal, Positive | Reservation) => {
(Safety::Safe, Some(attr_name), Safety::Safe, Positive | Reservation) => {
let span = tcx.def_span(def_id);
return Err(struct_span_code_err!(
tcx.dcx(),
@ -85,14 +85,14 @@ pub(super) fn check_item(
.emit());
}
(_, _, Unsafety::Unsafe, Negative) => {
(_, _, Safety::Unsafe, Negative) => {
// Reported in AST validation
assert!(tcx.dcx().has_errors().is_some(), "unsafe negative impl");
Ok(())
}
(_, _, Unsafety::Normal, Negative)
| (Unsafety::Unsafe, _, Unsafety::Unsafe, Positive | Reservation)
| (Unsafety::Normal, Some(_), Unsafety::Unsafe, Positive | Reservation)
| (Unsafety::Normal, None, Unsafety::Normal, _) => Ok(()),
(_, _, Safety::Safe, Negative)
| (Safety::Unsafe, _, Safety::Unsafe, Positive | Reservation)
| (Safety::Safe, Some(_), Safety::Unsafe, Positive | Reservation)
| (Safety::Safe, None, Safety::Safe, _) => Ok(()),
}
}

View file

@ -1102,11 +1102,11 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
let item = tcx.hir().expect_item(def_id);
let (is_auto, unsafety, items) = match item.kind {
hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
(is_auto == hir::IsAuto::Yes, unsafety, items)
let (is_auto, safety, items) = match item.kind {
hir::ItemKind::Trait(is_auto, safety, .., items) => {
(is_auto == hir::IsAuto::Yes, safety, items)
}
hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal, &[][..]),
hir::ItemKind::TraitAlias(..) => (false, hir::Safety::Safe, &[][..]),
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
};
@ -1247,7 +1247,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
ty::TraitDef {
def_id: def_id.to_def_id(),
unsafety,
safety,
paren_sugar,
has_auto_impl: is_auto,
is_marker,
@ -1286,7 +1286,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
{
icx.lowerer().lower_fn_ty(
hir_id,
sig.header.unsafety,
sig.header.safety,
sig.header.abi,
sig.decl,
Some(generics),
@ -1301,14 +1301,9 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
generics,
..
}) => icx.lowerer().lower_fn_ty(
hir_id,
header.unsafety,
header.abi,
decl,
Some(generics),
None,
),
}) => {
icx.lowerer().lower_fn_ty(hir_id, header.safety, header.abi, decl, Some(generics), None)
}
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
let abi = tcx.hir().get_foreign_abi(hir_id);
@ -1321,8 +1316,8 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<ty::PolyFnSig<
let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id).instantiate_identity());
// constructors for structs with `layout_scalar_valid_range` are unsafe to call
let safety = match tcx.layout_scalar_valid_range(adt_def_id) {
(Bound::Unbounded, Bound::Unbounded) => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
(Bound::Unbounded, Bound::Unbounded) => hir::Safety::Safe,
_ => hir::Safety::Unsafe,
};
ty::Binder::dummy(tcx.mk_fn_sig(inputs, ty, false, safety, abi::Abi::Rust))
}
@ -1409,13 +1404,13 @@ fn infer_return_ty_for_fn_sig<'tcx>(
fn_sig.inputs().iter().copied(),
recovered_ret_ty.unwrap_or_else(|| Ty::new_error(tcx, guar)),
fn_sig.c_variadic,
fn_sig.unsafety,
fn_sig.safety,
fn_sig.abi,
))
}
None => icx.lowerer().lower_fn_ty(
hir_id,
sig.header.unsafety,
sig.header.safety,
sig.header.abi,
sig.decl,
Some(generics),
@ -1574,7 +1569,7 @@ fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::ImplTrai
};
ty::ImplTraitHeader {
trait_ref: ty::EarlyBinder::bind(trait_ref),
unsafety: impl_.unsafety,
safety: impl_.safety,
polarity: polarity_of_impl(tcx, def_id, impl_, item.span)
}
})
@ -1685,14 +1680,14 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
decl: &'tcx hir::FnDecl<'tcx>,
abi: abi::Abi,
) -> ty::PolyFnSig<'tcx> {
let unsafety = if abi == abi::Abi::RustIntrinsic {
let safety = if abi == abi::Abi::RustIntrinsic {
intrinsic_operation_unsafety(tcx, def_id)
} else {
hir::Unsafety::Unsafe
hir::Safety::Unsafe
};
let hir_id = tcx.local_def_id_to_hir_id(def_id);
let fty =
ItemCtxt::new(tcx, def_id).lowerer().lower_fn_ty(hir_id, unsafety, abi, decl, None, None);
ItemCtxt::new(tcx, def_id).lowerer().lower_fn_ty(hir_id, safety, abi, decl, None, None);
// Feature gate SIMD types in FFI, since I am not sure that the
// ABIs are handled at all correctly. -huonw

View file

@ -2080,14 +2080,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Ty::new_fn_ptr(
tcx,
self.lower_fn_ty(
hir_ty.hir_id,
bf.unsafety,
bf.abi,
bf.decl,
None,
Some(hir_ty),
),
self.lower_fn_ty(hir_ty.hir_id, bf.safety, bf.abi, bf.decl, None, Some(hir_ty)),
)
}
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
@ -2309,11 +2302,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
/// Lower a function type from the HIR to our internal notion of a function signature.
#[instrument(level = "debug", skip(self, hir_id, unsafety, abi, decl, generics, hir_ty), ret)]
#[instrument(level = "debug", skip(self, hir_id, safety, abi, decl, generics, hir_ty), ret)]
pub fn lower_fn_ty(
&self,
hir_id: HirId,
unsafety: hir::Unsafety,
safety: hir::Safety,
abi: abi::Abi,
decl: &hir::FnDecl<'tcx>,
generics: Option<&hir::Generics<'_>>,
@ -2376,7 +2369,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
debug!(?output_ty);
let fn_ty = tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, unsafety, abi);
let fn_ty = tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, safety, abi);
let bare_fn_ty = ty::Binder::bind_with_vars(fn_ty, bound_vars);
if !self.allow_infer() && !(visitor.0.is_empty() && infer_replacements.is_empty()) {