Skip to content

Commit 3a933e5

Browse files
committed
Auto merge of #154412 - JonathanBrouwer:rollup-4EpQJse, r=JonathanBrouwer
Rollup of 2 pull requests Successful merges: - #154229 (Ensure `ErasedData` only implements appropriate auto traits) - #154409 (Update `try_blocks` to a new tracking issue number)
2 parents 3ea2fbc + e06b9bb commit 3a933e5

10 files changed

Lines changed: 47 additions & 26 deletions

File tree

compiler/rustc_errors/src/decorate_diag.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// This module provides types and traits for buffering lints until later in compilation.
22
use rustc_ast::node_id::NodeId;
33
use rustc_data_structures::fx::FxIndexMap;
4-
use rustc_data_structures::sync::DynSend;
4+
use rustc_data_structures::sync::{DynSend, DynSync};
55
use rustc_error_messages::MultiSpan;
66
use rustc_lint_defs::{BuiltinLintDiag, Lint, LintId};
77

@@ -10,7 +10,14 @@ use crate::{Diag, DiagCtxtHandle, Diagnostic, Level};
1010
/// We can't implement `Diagnostic` for `BuiltinLintDiag`, because decorating some of its
1111
/// variants requires types we don't have yet. So, handle that case separately.
1212
pub enum DecorateDiagCompat {
13-
Dynamic(Box<dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static>),
13+
Dynamic(
14+
Box<
15+
dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()>
16+
+ DynSync
17+
+ DynSend
18+
+ 'static,
19+
>,
20+
),
1421
Builtin(BuiltinLintDiag),
1522
}
1623

@@ -20,7 +27,7 @@ impl std::fmt::Debug for DecorateDiagCompat {
2027
}
2128
}
2229

23-
impl<D: for<'a> Diagnostic<'a, ()> + DynSend + 'static> From<D> for DecorateDiagCompat {
30+
impl<D: for<'a> Diagnostic<'a, ()> + DynSync + DynSend + 'static> From<D> for DecorateDiagCompat {
2431
#[inline]
2532
fn from(d: D) -> Self {
2633
Self::Dynamic(Box::new(|dcx, level| d.into_diag(dcx, level)))
@@ -83,7 +90,7 @@ impl LintBuffer {
8390
}
8491

8592
pub fn dyn_buffer_lint<
86-
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static,
93+
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSync + DynSend + 'static,
8794
>(
8895
&mut self,
8996
lint: &'static Lint,

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::panic;
77
use std::path::PathBuf;
88
use std::thread::panicking;
99

10-
use rustc_data_structures::sync::DynSend;
10+
use rustc_data_structures::sync::{DynSend, DynSync};
1111
use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg};
1212
use rustc_lint_defs::{Applicability, LintExpectationId};
1313
use rustc_macros::{Decodable, Encodable};
@@ -120,7 +120,9 @@ where
120120
}
121121

122122
impl<'a> Diagnostic<'a, ()>
123-
for Box<dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSend + 'static>
123+
for Box<
124+
dyn for<'b> FnOnce(DiagCtxtHandle<'b>, Level) -> Diag<'b, ()> + DynSync + DynSend + 'static,
125+
>
124126
{
125127
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
126128
self(dcx, level)

compiler/rustc_feature/src/unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ declare_features! (
693693
/// Allows inconsistent bounds in where clauses.
694694
(unstable, trivial_bounds, "1.28.0", Some(48214)),
695695
/// Allows using `try {...}` expressions.
696-
(unstable, try_blocks, "1.29.0", Some(31436)),
696+
(unstable, try_blocks, "1.29.0", Some(154391)),
697697
/// Allows using `try bikeshed TargetType {...}` expressions.
698698
(unstable, try_blocks_heterogeneous, "1.94.0", Some(149488)),
699699
/// Allows `impl Trait` to be used inside type aliases (RFC 2515).

compiler/rustc_middle/src/query/erase.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,37 @@
77
88
use std::ffi::OsStr;
99
use std::intrinsics::transmute_unchecked;
10+
use std::marker::PhantomData;
1011
use std::mem::MaybeUninit;
1112

1213
use rustc_ast::tokenstream::TokenStream;
1314
use rustc_data_structures::steal::Steal;
15+
use rustc_data_structures::sync::{DynSend, DynSync};
1416
use rustc_span::{ErrorGuaranteed, Spanned};
1517

1618
use crate::mir::mono::{MonoItem, NormalizationErrorInMono};
1719
use crate::ty::{self, Ty, TyCtxt};
1820
use crate::{mir, thir, traits};
1921

22+
unsafe extern "C" {
23+
type NoAutoTraits;
24+
}
25+
2026
/// Internal implementation detail of [`Erased`].
2127
#[derive(Copy, Clone)]
2228
pub struct ErasedData<Storage: Copy> {
2329
/// We use `MaybeUninit` here to make sure it's legal to store a transmuted
2430
/// value that isn't actually of type `Storage`.
2531
data: MaybeUninit<Storage>,
32+
/// `Storage` is an erased type, so we use an external type here to opt-out of auto traits
33+
/// as those would be incorrect.
34+
no_auto_traits: PhantomData<NoAutoTraits>,
2635
}
2736

37+
// SAFETY: The bounds on `erase_val` ensure the types we erase are `DynSync` and `DynSend`
38+
unsafe impl<Storage: Copy> DynSync for ErasedData<Storage> {}
39+
unsafe impl<Storage: Copy> DynSend for ErasedData<Storage> {}
40+
2841
/// Trait for types that can be erased into [`Erased<Self>`].
2942
///
3043
/// Erasing and unerasing values is performed by [`erase_val`] and [`restore_val`].
@@ -54,13 +67,11 @@ pub type Erased<T: Erasable> = ErasedData<impl Copy>;
5467
///
5568
/// `Erased<T>` and `Erased<U>` are type-checked as distinct types, but codegen
5669
/// can see whether they actually have the same storage type.
57-
///
58-
/// FIXME: This might have soundness issues with erasable types that don't
59-
/// implement the same auto-traits as `[u8; _]`; see
60-
/// <https://github.com/rust-lang/rust/pull/151715#discussion_r2740113250>
6170
#[inline(always)]
6271
#[define_opaque(Erased)]
63-
pub fn erase_val<T: Erasable>(value: T) -> Erased<T> {
72+
// The `DynSend` and `DynSync` bounds on `T` are used to
73+
// justify the safety of the implementations of these traits for `ErasedData`.
74+
pub fn erase_val<T: Erasable + DynSend + DynSync>(value: T) -> Erased<T> {
6475
// Ensure the sizes match
6576
const {
6677
if size_of::<T>() != size_of::<T::Storage>() {
@@ -78,6 +89,7 @@ pub fn erase_val<T: Erasable>(value: T) -> Erased<T> {
7889
//
7990
// SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes.
8091
data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Storage>>(value) },
92+
no_auto_traits: PhantomData,
8193
}
8294
}
8395

@@ -88,7 +100,7 @@ pub fn erase_val<T: Erasable>(value: T) -> Erased<T> {
88100
#[inline(always)]
89101
#[define_opaque(Erased)]
90102
pub fn restore_val<T: Erasable>(erased_value: Erased<T>) -> T {
91-
let ErasedData { data }: ErasedData<<T as Erasable>::Storage> = erased_value;
103+
let ErasedData { data, .. }: ErasedData<<T as Erasable>::Storage> = erased_value;
92104
// See comment in `erase_val` for why we use `transmute_unchecked`.
93105
//
94106
// SAFETY: Due to the use of impl Trait in `Erased` the only way to safely create an instance

compiler/rustc_session/src/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use rustc_ast::attr::AttrIdGenerator;
88
use rustc_ast::node_id::NodeId;
99
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
10-
use rustc_data_structures::sync::{AppendOnlyVec, DynSend, Lock};
10+
use rustc_data_structures::sync::{AppendOnlyVec, DynSend, DynSync, Lock};
1111
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
1212
use rustc_errors::emitter::{EmitterWithNote, stderr_destination};
1313
use rustc_errors::{
@@ -332,7 +332,7 @@ impl ParseSess {
332332
}
333333

334334
pub fn dyn_buffer_lint<
335-
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSend + 'static,
335+
F: for<'a> FnOnce(DiagCtxtHandle<'a>, Level) -> Diag<'a, ()> + DynSync + DynSend + 'static,
336336
>(
337337
&self,
338338
lint: &'static Lint,

src/doc/unstable-book/src/language-features/try-blocks.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# `try_blocks`
22

3-
The tracking issue for this feature is: [#31436]
3+
The tracking issue for this feature is: [#154391]
44

5-
[#31436]: https://github.com/rust-lang/rust/issues/31436
5+
[#154391]: https://github.com/rust-lang/rust/issues/154391
66

77
------------------------
88

@@ -14,14 +14,14 @@ block creates a new scope one can use the `?` operator in.
1414
1515
use std::num::ParseIntError;
1616
17-
let result: Result<i32, ParseIntError> = try {
17+
let result = try {
1818
"1".parse::<i32>()?
1919
+ "2".parse::<i32>()?
2020
+ "3".parse::<i32>()?
2121
};
2222
assert_eq!(result, Ok(6));
2323
24-
let result: Result<i32, ParseIntError> = try {
24+
let result = try {
2525
"1".parse::<i32>()?
2626
+ "foo".parse::<i32>()?
2727
+ "3".parse::<i32>()?

src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11576,9 +11576,9 @@ The tracking issue for this feature is: [#85731]
1157611576
label: "try_blocks",
1157711577
description: r##"# `try_blocks`
1157811578

11579-
The tracking issue for this feature is: [#31436]
11579+
The tracking issue for this feature is: [#154391]
1158011580

11581-
[#31436]: https://github.com/rust-lang/rust/issues/31436
11581+
[#154391]: https://github.com/rust-lang/rust/issues/154391
1158211582

1158311583
------------------------
1158411584

@@ -11590,14 +11590,14 @@ block creates a new scope one can use the `?` operator in.
1159011590

1159111591
use std::num::ParseIntError;
1159211592

11593-
let result: Result<i32, ParseIntError> = try {
11593+
let result = try {
1159411594
"1".parse::<i32>()?
1159511595
+ "2".parse::<i32>()?
1159611596
+ "3".parse::<i32>()?
1159711597
};
1159811598
assert_eq!(result, Ok(6));
1159911599

11600-
let result: Result<i32, ParseIntError> = try {
11600+
let result = try {
1160111601
"1".parse::<i32>()?
1160211602
+ "foo".parse::<i32>()?
1160311603
+ "3".parse::<i32>()?

tests/ui/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ Tests for the `#[doc(hidden)]` items.
14201420

14211421
## `tests/ui/try-block/`
14221422

1423-
`#![feature(try_blocks)]`. See [Tracking issue for `?` operator and `try` blocks (RFC 243, `question_mark` & `try_blocks` features)](https://github.com/rust-lang/rust/issues/31436).
1423+
`#![feature(try_blocks)]` & `#![feature(try_blocks_heterogeneous)]`. See [Tracking Issue for homogeneous `try_blocks`](https://github.com/rust-lang/rust/issues/154391) & [Experimental Tracking Issue for Heterogeneous Try Blocks](https://github.com/rust-lang/rust/issues/149488).
14241424

14251425
## `tests/ui/try-trait/`
14261426

tests/ui/feature-gates/feature-gate-try_blocks.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | x
88
LL | | };
99
| |_____^
1010
|
11-
= note: see issue #31436 <https://github.com/rust-lang/rust/issues/31436> for more information
11+
= note: see issue #154391 <https://github.com/rust-lang/rust/issues/154391> for more information
1212
= help: add `#![feature(try_blocks)]` to the crate attributes to enable
1313
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1414

tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: `try` blocks are unstable
44
LL | try {}
55
| ^^^^^^
66
|
7-
= note: see issue #31436 <https://github.com/rust-lang/rust/issues/31436> for more information
7+
= note: see issue #154391 <https://github.com/rust-lang/rust/issues/154391> for more information
88
= help: add `#![feature(try_blocks)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010
= warning: unstable syntax can change at any point in the future, causing a hard error!

0 commit comments

Comments
 (0)