Skip to content

Conversation

@compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Feb 6, 2025

This PR introduces a new, internal-only trait called BikeshedGuaranteedNoDrop1 to faithfully model the field check that used to be implemented manually by allowed_union_or_unsafe_field.

fn allowed_union_or_unsafe_field<'tcx>(
tcx: TyCtxt<'tcx>,
ty: Ty<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
span: Span,
) -> bool {
// We don't just accept all !needs_drop fields, due to semver concerns.
let allowed = match ty.kind() {
ty::Ref(..) => true, // references never drop (even mutable refs, which are non-Copy and hence fail the later check)
ty::Tuple(tys) => {
// allow tuples of allowed types
tys.iter().all(|ty| allowed_union_or_unsafe_field(tcx, ty, typing_env, span))
}
ty::Array(elem, _len) => {
// Like `Copy`, we do *not* special-case length 0.
allowed_union_or_unsafe_field(tcx, *elem, typing_env, span)
}
_ => {
// Fallback case: allow `ManuallyDrop` and things that are `Copy`,
// also no need to report an error if the type is unresolved.
ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop())
|| tcx.type_is_copy_modulo_regions(typing_env, ty)
|| ty.references_error()
}
};
if allowed && ty.needs_drop(tcx, typing_env) {
// This should never happen. But we can get here e.g. in case of name resolution errors.
tcx.dcx()
.span_delayed_bug(span, "we should never accept maybe-dropping union or unsafe fields");
}
allowed
}

Copying over the doc comment from the trait:

/// Marker trait for the types that are allowed in union fields, unsafe fields,
/// and unsafe binder types.
///
/// Implemented for:
/// * `&T`, `&mut T` for all `T`,
/// * `ManuallyDrop<T>` for all `T`,
/// * tuples and arrays whose elements implement `BikeshedGuaranteedNoDrop`,
/// * or otherwise, all types that are `Copy`.
///
/// Notably, this doesn't include all trivially-destructible types for semver
/// reasons.
///
/// Bikeshed name for now.

As far as I am aware, there's no new behavior being guaranteed by this trait, since it operates the same as the manually implemented check. We could easily rip out this trait and go back to using the manually implemented check for union fields, however using a trait means that this code can be shared by WF for unsafe<> binders too. See the last commit.

The only diagnostic changes are that this now fires false-negatives for fields that are ill-formed. I don't consider that to be much of a problem though.

r? oli-obk

Footnotes

  1. Please let's not bikeshed this name lol. There's no good name for ValidForUnsafeFieldsUnsafeBindersAndUnionFields.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 6, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 6, 2025

changes to the core type system

cc @compiler-errors, @lcnr

@rustbot rustbot added T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Feb 6, 2025
@@ -1,40 +1,40 @@
//@ known-bug: unknown
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of random fixes in this file to make it compile lol

@compiler-errors
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 6, 2025
@bors
Copy link
Collaborator

bors commented Feb 6, 2025

⌛ Trying commit 885f835 with merge 0a914ae...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Feb 6, 2025

☀️ Try build successful - checks-actions
Build commit: 0a914ae (0a914aea5a53390f79db9a8f7c4d6848cd98bd3e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0a914ae): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 1.1%, secondary 2.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.1% [0.9%, 1.4%] 2
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [0.9%, 1.4%] 2

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 780.473s -> 780.252s (-0.03%)
Artifact size: 329.02 MiB -> 329.03 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 7, 2025
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, otherwise r=me

@compiler-errors
Copy link
Member Author

Regarding the codegen tests (which has a surprising number of no_core tests that don't use minicore stubs, and which changing all of them is a headache) -- I'm gonna modify this to fall back to the Copy lang item if the BikeshedGuaranteedNoDrop lang item is missing.

@compiler-errors compiler-errors force-pushed the BikeshedGuaranteedNoDrop branch from 885f835 to 86cc322 Compare February 7, 2025 17:41
@rust-log-analyzer

This comment has been minimized.

@compiler-errors compiler-errors force-pushed the BikeshedGuaranteedNoDrop branch from 86cc322 to 4c17921 Compare February 7, 2025 18:36
@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

@compiler-errors compiler-errors force-pushed the BikeshedGuaranteedNoDrop branch from 4c17921 to 5c67cfa Compare February 8, 2025 23:10
@compiler-errors
Copy link
Member Author

@bors r=lcnr

@bors
Copy link
Collaborator

bors commented Feb 9, 2025

📌 Commit 5c67cfa has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2025
@Noratrieb
Copy link
Member

@bors rollup=maybe perf neutral

@jhpratt
Copy link
Member

jhpratt commented Feb 13, 2025

@bors r- rollup=iffy

failing tidy in #136952

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 13, 2025
@compiler-errors compiler-errors force-pushed the BikeshedGuaranteedNoDrop branch from 5c67cfa to d0564fd Compare February 13, 2025 03:45
@compiler-errors
Copy link
Member Author

@bors r=lcnr rollup=maybe

@bors
Copy link
Collaborator

bors commented Feb 13, 2025

📌 Commit d0564fd has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 13, 2025
@bors bors merged commit 4ea2610 into rust-lang:master Feb 13, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Feb 13, 2025
@compiler-errors compiler-errors deleted the BikeshedGuaranteedNoDrop branch March 14, 2025 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants