Skip to content

Commit 33c245b

Browse files
committed
Auto merge of rust-lang#134125 - fmease:rollup-u38o3ob, r=fmease
Rollup of 11 pull requests Successful merges: - rust-lang#133478 (jsondocck: Parse, don't validate commands.) - rust-lang#133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates) - rust-lang#133970 ([AIX] Replace sa_sigaction with sa_union.__su_sigaction for AIX) - rust-lang#133980 ([AIX] Remove option "-n" from AIX "ln" command) - rust-lang#134008 (Make `Copy` unsafe to implement for ADTs with `unsafe` fields) - rust-lang#134017 (Don't use `AsyncFnOnce::CallOnceFuture` bounds for signature deduction) - rust-lang#134023 (handle cygwin environment in `install::sanitize_sh`) - rust-lang#134041 (Use SourceMap to load debugger visualizer files) - rust-lang#134065 (Move `write_graphviz_results`) - rust-lang#134106 (Add compiler-maintainers who requested to be on review rotation) - rust-lang#134123 (bootstrap: Forward cargo JSON output to stdout, not stderr) Failed merges: - rust-lang#134120 (Remove Felix from ping groups and review rotation) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4996052 + c42c248 commit 33c245b

File tree

31 files changed

+604
-536
lines changed

31 files changed

+604
-536
lines changed

compiler/rustc_codegen_cranelift/example/mini_core.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ impl<T: ?Sized> LegacyReceiver for &mut T {}
5555
impl<T: ?Sized> LegacyReceiver for Box<T> {}
5656

5757
#[lang = "copy"]
58-
pub unsafe trait Copy {}
59-
60-
unsafe impl Copy for bool {}
61-
unsafe impl Copy for u8 {}
62-
unsafe impl Copy for u16 {}
63-
unsafe impl Copy for u32 {}
64-
unsafe impl Copy for u64 {}
65-
unsafe impl Copy for u128 {}
66-
unsafe impl Copy for usize {}
67-
unsafe impl Copy for i8 {}
68-
unsafe impl Copy for i16 {}
69-
unsafe impl Copy for i32 {}
70-
unsafe impl Copy for isize {}
71-
unsafe impl Copy for f32 {}
72-
unsafe impl Copy for f64 {}
73-
unsafe impl Copy for char {}
74-
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
75-
unsafe impl<T: ?Sized> Copy for *const T {}
76-
unsafe impl<T: ?Sized> Copy for *mut T {}
77-
unsafe impl<T: Copy> Copy for Option<T> {}
58+
pub trait Copy {}
59+
60+
impl Copy for bool {}
61+
impl Copy for u8 {}
62+
impl Copy for u16 {}
63+
impl Copy for u32 {}
64+
impl Copy for u64 {}
65+
impl Copy for u128 {}
66+
impl Copy for usize {}
67+
impl Copy for i8 {}
68+
impl Copy for i16 {}
69+
impl Copy for i32 {}
70+
impl Copy for isize {}
71+
impl Copy for f32 {}
72+
impl Copy for f64 {}
73+
impl Copy for char {}
74+
impl<'a, T: ?Sized> Copy for &'a T {}
75+
impl<T: ?Sized> Copy for *const T {}
76+
impl<T: ?Sized> Copy for *mut T {}
77+
impl<T: Copy> Copy for Option<T> {}
7878

7979
#[lang = "sync"]
8080
pub unsafe trait Sync {}

compiler/rustc_codegen_gcc/example/mini_core.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ impl<T: ?Sized> LegacyReceiver for &mut T {}
5252
impl<T: ?Sized, A: Allocator> LegacyReceiver for Box<T, A> {}
5353

5454
#[lang = "copy"]
55-
pub unsafe trait Copy {}
56-
57-
unsafe impl Copy for bool {}
58-
unsafe impl Copy for u8 {}
59-
unsafe impl Copy for u16 {}
60-
unsafe impl Copy for u32 {}
61-
unsafe impl Copy for u64 {}
62-
unsafe impl Copy for usize {}
63-
unsafe impl Copy for i8 {}
64-
unsafe impl Copy for i16 {}
65-
unsafe impl Copy for i32 {}
66-
unsafe impl Copy for isize {}
67-
unsafe impl Copy for f32 {}
68-
unsafe impl Copy for f64 {}
69-
unsafe impl Copy for char {}
70-
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
71-
unsafe impl<T: ?Sized> Copy for *const T {}
72-
unsafe impl<T: ?Sized> Copy for *mut T {}
55+
pub trait Copy {}
56+
57+
impl Copy for bool {}
58+
impl Copy for u8 {}
59+
impl Copy for u16 {}
60+
impl Copy for u32 {}
61+
impl Copy for u64 {}
62+
impl Copy for usize {}
63+
impl Copy for i8 {}
64+
impl Copy for i16 {}
65+
impl Copy for i32 {}
66+
impl Copy for isize {}
67+
impl Copy for f32 {}
68+
impl Copy for f64 {}
69+
impl Copy for char {}
70+
impl<'a, T: ?Sized> Copy for &'a T {}
71+
impl<T: ?Sized> Copy for *const T {}
72+
impl<T: ?Sized> Copy for *mut T {}
7373

7474
#[lang = "sync"]
7575
pub unsafe trait Sync {}

compiler/rustc_codegen_ssa/src/back/link.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,15 @@ fn add_upstream_rust_crates(
27452745
.find(|(ty, _)| *ty == crate_type)
27462746
.expect("failed to find crate type in dependency format list");
27472747

2748+
if sess.target.is_like_aix {
2749+
// Unlike ELF linkers, AIX doesn't feature `DT_SONAME` to override
2750+
// the dependency name when outputing a shared library. Thus, `ld` will
2751+
// use the full path to shared libraries as the dependency if passed it
2752+
// by default unless `noipath` is passed.
2753+
// https://www.ibm.com/docs/en/aix/7.3?topic=l-ld-command.
2754+
cmd.link_or_cc_arg("-bnoipath");
2755+
}
2756+
27482757
for &cnum in &codegen_results.crate_info.used_crates {
27492758
// We may not pass all crates through to the linker. Some crates may appear statically in
27502759
// an existing dylib, meaning we'll pick up all the symbols from the dylib.

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
103103
}
104104

105105
let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
106-
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) {
106+
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause, impl_header.safety) {
107107
Ok(()) => Ok(()),
108108
Err(CopyImplementationError::InfringingFields(fields)) => {
109109
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
@@ -123,6 +123,12 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
123123
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
124124
Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span }))
125125
}
126+
Err(CopyImplementationError::HasUnsafeFields) => {
127+
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
128+
Err(tcx
129+
.dcx()
130+
.span_delayed_bug(span, format!("cannot implement `Copy` for `{}`", self_type)))
131+
}
126132
}
127133
}
128134

compiler/rustc_hir_analysis/src/coherence/unsafety.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
6-
use rustc_hir::Safety;
6+
use rustc_hir::{LangItem, Safety};
77
use rustc_middle::ty::ImplPolarity::*;
88
use rustc_middle::ty::print::PrintTraitRefExt as _;
99
use rustc_middle::ty::{ImplTraitHeader, TraitDef, TyCtxt};
@@ -20,7 +20,19 @@ pub(super) fn check_item(
2020
tcx.generics_of(def_id).own_params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
2121
let trait_ref = trait_header.trait_ref.instantiate_identity();
2222

23-
match (trait_def.safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
23+
let is_copy = tcx.is_lang_item(trait_def.def_id, LangItem::Copy);
24+
let trait_def_safety = if is_copy {
25+
// If `Self` has unsafe fields, `Copy` is unsafe to implement.
26+
if trait_header.trait_ref.skip_binder().self_ty().has_unsafe_fields() {
27+
rustc_hir::Safety::Unsafe
28+
} else {
29+
rustc_hir::Safety::Safe
30+
}
31+
} else {
32+
trait_def.safety
33+
};
34+
35+
match (trait_def_safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
2436
(Safety::Safe, None, Safety::Unsafe, Positive | Reservation) => {
2537
let span = tcx.def_span(def_id);
2638
return Err(struct_span_code_err!(
@@ -48,12 +60,22 @@ pub(super) fn check_item(
4860
"the trait `{}` requires an `unsafe impl` declaration",
4961
trait_ref.print_trait_sugared()
5062
)
51-
.with_note(format!(
52-
"the trait `{}` enforces invariants that the compiler can't check. \
53-
Review the trait documentation and make sure this implementation \
54-
upholds those invariants before adding the `unsafe` keyword",
55-
trait_ref.print_trait_sugared()
56-
))
63+
.with_note(if is_copy {
64+
format!(
65+
"the trait `{}` cannot be safely implemented for `{}` \
66+
because it has unsafe fields. Review the invariants \
67+
of those fields before adding an `unsafe impl`",
68+
trait_ref.print_trait_sugared(),
69+
trait_ref.self_ty(),
70+
)
71+
} else {
72+
format!(
73+
"the trait `{}` enforces invariants that the compiler can't check. \
74+
Review the trait documentation and make sure this implementation \
75+
upholds those invariants before adding the `unsafe` keyword",
76+
trait_ref.print_trait_sugared()
77+
)
78+
})
5779
.with_span_suggestion_verbose(
5880
span.shrink_to_lo(),
5981
"add `unsafe` to this trait implementation",

compiler/rustc_hir_typeck/src/closure.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -454,28 +454,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
454454
closure_kind: hir::ClosureKind,
455455
projection: ty::PolyProjectionPredicate<'tcx>,
456456
) -> Option<ExpectedSig<'tcx>> {
457-
let tcx = self.tcx;
458-
459-
let trait_def_id = projection.trait_def_id(tcx);
457+
let def_id = projection.projection_def_id();
460458

461459
// For now, we only do signature deduction based off of the `Fn` and `AsyncFn` traits,
462460
// for closures and async closures, respectively.
463461
match closure_kind {
464-
hir::ClosureKind::Closure
465-
if self.tcx.fn_trait_kind_from_def_id(trait_def_id).is_some() =>
466-
{
462+
hir::ClosureKind::Closure if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) => {
467463
self.extract_sig_from_projection(cause_span, projection)
468464
}
469465
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
470-
if self.tcx.async_fn_trait_kind_from_def_id(trait_def_id).is_some() =>
466+
if self.tcx.is_lang_item(def_id, LangItem::AsyncFnOnceOutput) =>
471467
{
472468
self.extract_sig_from_projection(cause_span, projection)
473469
}
474470
// It's possible we've passed the closure to a (somewhat out-of-fashion)
475471
// `F: FnOnce() -> Fut, Fut: Future<Output = T>` style bound. Let's still
476472
// guide inference here, since it's beneficial for the user.
477473
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
478-
if self.tcx.fn_trait_kind_from_def_id(trait_def_id).is_some() =>
474+
if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) =>
479475
{
480476
self.extract_sig_from_projection_and_future_bound(cause_span, projection)
481477
}

compiler/rustc_lint/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
625625
cx.param_env,
626626
ty,
627627
traits::ObligationCause::misc(item.span, item.owner_id.def_id),
628+
hir::Safety::Safe,
628629
)
629630
.is_ok()
630631
{

compiler/rustc_middle/src/ty/sty.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,7 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
980980
}
981981

982982
fn has_unsafe_fields(self) -> bool {
983-
if let ty::Adt(adt_def, ..) = self.kind() {
984-
adt_def.all_fields().any(|x| x.safety == hir::Safety::Unsafe)
985-
} else {
986-
false
987-
}
983+
Ty::has_unsafe_fields(self)
988984
}
989985
}
990986

compiler/rustc_middle/src/ty/util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,15 @@ impl<'tcx> Ty<'tcx> {
12881288
}
12891289
}
12901290

1291+
/// Checks whether this type is an ADT that has unsafe fields.
1292+
pub fn has_unsafe_fields(self) -> bool {
1293+
if let ty::Adt(adt_def, ..) = self.kind() {
1294+
adt_def.all_fields().any(|x| x.safety == hir::Safety::Unsafe)
1295+
} else {
1296+
false
1297+
}
1298+
}
1299+
12911300
/// Get morphology of the async drop glue, needed for types which do not
12921301
/// use async drop. To get async drop glue morphology for a definition see
12931302
/// [`TyCtxt::async_drop_glue_morphology`]. Used for `AsyncDestruct::Destructor`

0 commit comments

Comments
 (0)