Skip to content

Commit 11a73f6

Browse files
committed
Store impl unsafety in impl trait header
1 parent 09d73fa commit 11a73f6

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

compiler/rustc_hir_analysis/src/coherence/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed>
132132
let mut res = tcx.ensure().specialization_graph_of(def_id);
133133

134134
for &impl_def_id in impls {
135-
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
135+
let trait_header = tcx.impl_trait_header(impl_def_id).unwrap().instantiate_identity();
136136

137-
res = res.and(check_impl(tcx, impl_def_id, trait_ref));
138-
res = res.and(check_object_overlap(tcx, impl_def_id, trait_ref));
137+
res = res.and(check_impl(tcx, impl_def_id, trait_header.trait_ref));
138+
res = res.and(check_object_overlap(tcx, impl_def_id, trait_header.trait_ref));
139139

140-
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_ref));
140+
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header));
141141
res = res.and(tcx.ensure().orphan_check_impl(impl_def_id));
142142
res = res.and(builtin::check_trait(tcx, def_id, impl_def_id));
143143
}

compiler/rustc_hir_analysis/src/coherence/unsafety.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
//! crate or pertains to a type defined in this crate.
33
44
use rustc_errors::{codes::*, struct_span_code_err};
5-
use rustc_hir as hir;
65
use rustc_hir::Unsafety;
7-
use rustc_middle::ty::{TraitRef, TyCtxt};
6+
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TyCtxt};
87
use rustc_span::def_id::LocalDefId;
98
use rustc_span::ErrorGuaranteed;
109

1110
pub(super) fn check_item(
1211
tcx: TyCtxt<'_>,
1312
def_id: LocalDefId,
14-
trait_ref: TraitRef<'_>,
13+
trait_header: ImplTraitHeader<'_>,
1514
) -> Result<(), ErrorGuaranteed> {
16-
let item = tcx.hir().expect_item(def_id);
17-
let impl_ = item.expect_impl();
15+
let trait_ref = trait_header.trait_ref;
1816
let trait_def = tcx.trait_def(trait_ref.def_id);
19-
let unsafe_attr = impl_.generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
20-
match (trait_def.unsafety, unsafe_attr, impl_.unsafety, impl_.polarity) {
21-
(Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
17+
let unsafe_attr =
18+
tcx.generics_of(def_id).params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
19+
match (trait_def.unsafety, unsafe_attr, trait_header.unsafety, trait_header.polarity) {
20+
(Unsafety::Normal, None, Unsafety::Unsafe, Positive | Reservation) => {
21+
let span = tcx.def_span(def_id);
2222
return Err(struct_span_code_err!(
2323
tcx.dcx(),
2424
tcx.def_span(def_id),
@@ -27,18 +27,19 @@ pub(super) fn check_item(
2727
trait_ref.print_trait_sugared()
2828
)
2929
.with_span_suggestion_verbose(
30-
item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)),
30+
span.with_hi(span.lo() + rustc_span::BytePos(7)),
3131
"remove `unsafe` from this trait implementation",
3232
"",
3333
rustc_errors::Applicability::MachineApplicable,
3434
)
3535
.emit());
3636
}
3737

38-
(Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
38+
(Unsafety::Unsafe, _, Unsafety::Normal, Positive | Reservation) => {
39+
let span = tcx.def_span(def_id);
3940
return Err(struct_span_code_err!(
4041
tcx.dcx(),
41-
tcx.def_span(def_id),
42+
span,
4243
E0200,
4344
"the trait `{}` requires an `unsafe impl` declaration",
4445
trait_ref.print_trait_sugared()
@@ -50,18 +51,19 @@ pub(super) fn check_item(
5051
trait_ref.print_trait_sugared()
5152
))
5253
.with_span_suggestion_verbose(
53-
item.span.shrink_to_lo(),
54+
span.shrink_to_lo(),
5455
"add `unsafe` to this trait implementation",
5556
"unsafe ",
5657
rustc_errors::Applicability::MaybeIncorrect,
5758
)
5859
.emit());
5960
}
6061

61-
(Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => {
62+
(Unsafety::Normal, Some(attr_name), Unsafety::Normal, Positive | Reservation) => {
63+
let span = tcx.def_span(def_id);
6264
return Err(struct_span_code_err!(
6365
tcx.dcx(),
64-
tcx.def_span(def_id),
66+
span,
6567
E0569,
6668
"requires an `unsafe impl` declaration due to `#[{}]` attribute",
6769
attr_name
@@ -73,22 +75,22 @@ pub(super) fn check_item(
7375
trait_ref.print_trait_sugared()
7476
))
7577
.with_span_suggestion_verbose(
76-
item.span.shrink_to_lo(),
78+
span.shrink_to_lo(),
7779
"add `unsafe` to this trait implementation",
7880
"unsafe ",
7981
rustc_errors::Applicability::MaybeIncorrect,
8082
)
8183
.emit());
8284
}
8385

84-
(_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
86+
(_, _, Unsafety::Unsafe, Negative) => {
8587
// Reported in AST validation
86-
tcx.dcx().span_delayed_bug(item.span, "unsafe negative impl");
88+
tcx.dcx().span_delayed_bug(tcx.def_span(def_id), "unsafe negative impl");
8789
Ok(())
8890
}
89-
(_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
90-
| (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
91-
| (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
91+
(_, _, Unsafety::Normal, Negative)
92+
| (Unsafety::Unsafe, _, Unsafety::Unsafe, Positive | Reservation)
93+
| (Unsafety::Normal, Some(_), Unsafety::Unsafe, Positive | Reservation)
9294
| (Unsafety::Normal, None, Unsafety::Normal, _) => Ok(()),
9395
}
9496
}

compiler/rustc_hir_analysis/src/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ fn impl_trait_header(
15391539
};
15401540
ty::EarlyBinder::bind(ty::ImplTraitHeader {
15411541
trait_ref,
1542+
unsafety: impl_.unsafety,
15421543
polarity: polarity_of_impl(tcx, def_id, impl_, item.span)
15431544
})
15441545
})

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub struct ImplHeader<'tcx> {
252252
pub struct ImplTraitHeader<'tcx> {
253253
pub trait_ref: ty::TraitRef<'tcx>,
254254
pub polarity: ImplPolarity,
255+
pub unsafety: hir::Unsafety,
255256
}
256257

257258
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)]

0 commit comments

Comments
 (0)