2014-12-10 15:59:20 +00:00
|
|
|
//! Unsafety checker: every impl either implements a trait defined in this
|
|
|
|
//! crate or pertains to a type defined in this crate.
|
|
|
|
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-13 23:57:07 +00:00
|
|
|
use rustc_errors::{codes::*, struct_span_code_err};
|
2024-05-17 17:17:48 +00:00
|
|
|
use rustc_hir::Safety;
|
2024-05-10 18:59:56 +00:00
|
|
|
use rustc_middle::ty::print::PrintTraitRefExt as _;
|
2024-02-13 09:29:31 +00:00
|
|
|
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TraitDef, TyCtxt};
|
2021-05-09 18:53:13 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2024-01-23 15:23:22 +00:00
|
|
|
use rustc_span::ErrorGuaranteed;
|
2014-12-10 15:59:20 +00:00
|
|
|
|
2024-02-09 20:33:23 +00:00
|
|
|
pub(super) fn check_item(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
def_id: LocalDefId,
|
2024-02-11 10:43:01 +00:00
|
|
|
trait_header: ImplTraitHeader<'_>,
|
2024-02-13 09:29:31 +00:00
|
|
|
trait_def: &TraitDef,
|
2024-02-09 20:33:23 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2024-02-11 10:43:01 +00:00
|
|
|
let unsafe_attr =
|
2024-05-10 00:56:44 +00:00
|
|
|
tcx.generics_of(def_id).own_params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
|
2024-03-05 19:19:05 +00:00
|
|
|
let trait_ref = trait_header.trait_ref.instantiate_identity();
|
|
|
|
|
2024-05-17 17:17:48 +00:00
|
|
|
match (trait_def.safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
|
|
|
|
(Safety::Safe, None, Safety::Unsafe, Positive | Reservation) => {
|
2024-02-11 10:43:01 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2024-02-09 20:33:23 +00:00
|
|
|
return Err(struct_span_code_err!(
|
|
|
|
tcx.dcx(),
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
E0199,
|
|
|
|
"implementing the trait `{}` is not unsafe",
|
|
|
|
trait_ref.print_trait_sugared()
|
|
|
|
)
|
|
|
|
.with_span_suggestion_verbose(
|
2024-02-11 10:43:01 +00:00
|
|
|
span.with_hi(span.lo() + rustc_span::BytePos(7)),
|
2024-02-09 20:33:23 +00:00
|
|
|
"remove `unsafe` from this trait implementation",
|
|
|
|
"",
|
|
|
|
rustc_errors::Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit());
|
|
|
|
}
|
2014-12-10 15:59:20 +00:00
|
|
|
|
2024-05-17 17:17:48 +00:00
|
|
|
(Safety::Unsafe, _, Safety::Safe, Positive | Reservation) => {
|
2024-02-11 10:43:01 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2024-02-09 20:33:23 +00:00
|
|
|
return Err(struct_span_code_err!(
|
|
|
|
tcx.dcx(),
|
2024-02-11 10:43:01 +00:00
|
|
|
span,
|
2024-02-09 20:33:23 +00:00
|
|
|
E0200,
|
|
|
|
"the trait `{}` requires an `unsafe impl` declaration",
|
|
|
|
trait_ref.print_trait_sugared()
|
|
|
|
)
|
|
|
|
.with_note(format!(
|
|
|
|
"the trait `{}` enforces invariants that the compiler can't check. \
|
2022-10-20 02:06:50 +00:00
|
|
|
Review the trait documentation and make sure this implementation \
|
|
|
|
upholds those invariants before adding the `unsafe` keyword",
|
2024-02-09 20:33:23 +00:00
|
|
|
trait_ref.print_trait_sugared()
|
|
|
|
))
|
|
|
|
.with_span_suggestion_verbose(
|
2024-02-11 10:43:01 +00:00
|
|
|
span.shrink_to_lo(),
|
2024-02-09 20:33:23 +00:00
|
|
|
"add `unsafe` to this trait implementation",
|
|
|
|
"unsafe ",
|
|
|
|
rustc_errors::Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit());
|
|
|
|
}
|
2014-12-10 15:59:20 +00:00
|
|
|
|
2024-05-17 17:17:48 +00:00
|
|
|
(Safety::Safe, Some(attr_name), Safety::Safe, Positive | Reservation) => {
|
2024-02-11 10:43:01 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2024-02-09 20:33:23 +00:00
|
|
|
return Err(struct_span_code_err!(
|
|
|
|
tcx.dcx(),
|
2024-02-11 10:43:01 +00:00
|
|
|
span,
|
2024-02-09 20:33:23 +00:00
|
|
|
E0569,
|
|
|
|
"requires an `unsafe impl` declaration due to `#[{}]` attribute",
|
|
|
|
attr_name
|
|
|
|
)
|
|
|
|
.with_note(format!(
|
|
|
|
"the trait `{}` enforces invariants that the compiler can't check. \
|
2022-10-20 02:06:50 +00:00
|
|
|
Review the trait documentation and make sure this implementation \
|
|
|
|
upholds those invariants before adding the `unsafe` keyword",
|
2024-02-09 20:33:23 +00:00
|
|
|
trait_ref.print_trait_sugared()
|
|
|
|
))
|
|
|
|
.with_span_suggestion_verbose(
|
2024-02-11 10:43:01 +00:00
|
|
|
span.shrink_to_lo(),
|
2024-02-09 20:33:23 +00:00
|
|
|
"add `unsafe` to this trait implementation",
|
|
|
|
"unsafe ",
|
|
|
|
rustc_errors::Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit());
|
|
|
|
}
|
2016-10-12 12:23:38 +00:00
|
|
|
|
2024-05-17 17:17:48 +00:00
|
|
|
(_, _, Safety::Unsafe, Negative) => {
|
2024-02-09 20:33:23 +00:00
|
|
|
// Reported in AST validation
|
2024-02-14 04:17:15 +00:00
|
|
|
assert!(tcx.dcx().has_errors().is_some(), "unsafe negative impl");
|
2024-02-09 20:33:23 +00:00
|
|
|
Ok(())
|
2015-03-11 23:53:55 +00:00
|
|
|
}
|
2024-05-17 17:17:48 +00:00
|
|
|
(_, _, Safety::Safe, Negative)
|
|
|
|
| (Safety::Unsafe, _, Safety::Unsafe, Positive | Reservation)
|
|
|
|
| (Safety::Safe, Some(_), Safety::Unsafe, Positive | Reservation)
|
|
|
|
| (Safety::Safe, None, Safety::Safe, _) => Ok(()),
|
2015-03-11 23:53:55 +00:00
|
|
|
}
|
|
|
|
}
|