Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
1cac5fa
Look for `python3` first on MacOS, not `py`
jyn514 Feb 13, 2025
477a2ee
std::fs: slightly reformat `remove_dir_all` error docs
jieyouxu Feb 18, 2025
2c752bc
Undeprecate env::home_dir
arlosi Feb 20, 2025
9323ba5
Remove MaybeForgetReturn suggestion
compiler-errors Feb 20, 2025
ef337a6
Make `ptr_cast_add_auto_to_object` lint into hard error
traviscross Feb 9, 2025
a7bd4a3
Add DWARF test case for non-C-like `repr128` enums
beetrees Feb 25, 2025
b2bb7cc
Fix char count in Display for ByteStr
thaliaarchi Feb 28, 2025
41dd80a
add test to reproduce #137662 (using ty decl macro fragment in an att…
jdonszelmann Feb 26, 2025
476b098
adjust Layout debug printing to match the internal field names
RalfJung Feb 28, 2025
91034ad
Do not require that unsafe fields lack drop glue
jswrenn Feb 28, 2025
a8bff87
Stabilize [T]::split_off... methods
cramertj Feb 28, 2025
38b364b
rustc_fluent_macro: use CARGO_CRATE_NAME instead of CARGO_PKG_NAME
lolbinarycat Mar 1, 2025
3786a7a
stabilize `box_uninit_write`
slanterns Mar 1, 2025
3998690
compiletest: remove legacy `Makefile`-based `run-make` support
jieyouxu Feb 5, 2025
413b824
run-make: remove `tools.mk`
jieyouxu Feb 5, 2025
9b17c98
run-make: update test suite README
jieyouxu Feb 5, 2025
ed168e7
run-make-support: remove outdated comments
jieyouxu Mar 1, 2025
efec638
tidy: remove legacy `Makefile` checks
jieyouxu Feb 5, 2025
b0d6a84
rustc-dev-guide: remove mentions of legacy `Makefile` run-make infra
jieyouxu Feb 5, 2025
95b030f
triagebot: stop backlinking to the test porting tracking issue
jieyouxu Feb 5, 2025
0ad48b4
Do not recover missing lifetime with random in-scope lifetime
compiler-errors Mar 3, 2025
e403654
Simplify `<Postorder as Iterator>::size_hint`
scottmcm Mar 3, 2025
7e0a2fc
compiletest: change `TEST_BUILD_DIR` to maximally normalize
jieyouxu Feb 11, 2025
8342914
tests: remove explicit long type filename hash normalization from som…
jieyouxu Feb 11, 2025
12cd0f1
tests: rebless some tests as a side-effect of `TEST_BUILD_DIR` changes
jieyouxu Feb 11, 2025
4f2a3dc
tests: manually rebless `tests/ui-fulldeps/codegen-backend/hotplug.rs`
jieyouxu Feb 11, 2025
133705c
[rustdoc] hide item that is not marked as doc(inline) and whose src i…
xizheyin Feb 27, 2025
a23566a
Provide more context on resolve error caused from incorrect RTN
estebank Feb 28, 2025
53ee696
Update MSVC INSTALL.md instructions to recommend VS 2022 + recent Win…
jieyouxu Mar 3, 2025
a89cddb
Add ``dyn`` keyword
Eclips4 Mar 3, 2025
ab31129
Point of macro expansion from call expr if it involves macro var
compiler-errors Feb 24, 2025
e4dfca8
Point out macro expansion ident in resolver errors too
compiler-errors Feb 24, 2025
0607246
Fix associated type errors too
compiler-errors Feb 24, 2025
4fdc1b5
Construct MIR error body for global_asm correctly
compiler-errors Feb 23, 2025
dc9d559
Exclude global_asm from mir_keys
compiler-errors Feb 23, 2025
09e5846
Also note struct access, and fix macro expansion from foreign crates
compiler-errors Feb 24, 2025
3e5fddc
Allow struct field default values to reference struct's generics
compiler-errors Mar 3, 2025
12cc2b9
Remove unused `PpMode::needs_hir`
Zalathar Mar 4, 2025
d1d0b9b
add run-make test for rustc_fluent_macro with hypens in package name
lolbinarycat Mar 3, 2025
fe6cf34
Also simplify `Preorder`'s `size_hint`
scottmcm Mar 4, 2025
4febd27
Fix some typos
fuyangpengqi Mar 4, 2025
69c4651
ci: use ubuntu 24 on arm large runner
marcoieni Mar 4, 2025
2816f03
Add `avr-none` to SUMMARY.md and platform-support.md
tgross35 Mar 4, 2025
84dd2a7
Remove obsolete comment from DeduceReadOnly
tmiasko Mar 4, 2025
c6c4ea4
Revert "compiler/rustc_data_structures/src/sync/worker_local.rs: dele…
safinaskar Mar 4, 2025
da3e736
Clarify why InhabitedPredicate::instantiate_opt exists
meithecatte Feb 28, 2025
a3d63fb
InhabitedPredicate: avoid using a wildcard branch
meithecatte Mar 3, 2025
c392cf7
Added documentation for flushing
pcorwin Feb 10, 2025
3d62b27
Ensure that negative auto impls are always applicable
compiler-errors Feb 28, 2025
05a8060
Make rustdoc tests use always applicable negative auto impls
compiler-errors Feb 28, 2025
2568493
Rollup merge of #136581 - jieyouxu:makefile-be-gone, r=Kobzol
workingjubilee Mar 4, 2025
0b5df26
Rollup merge of #136764 - traviscross:TC/make-ptr_cast_add_auto_to_ob…
workingjubilee Mar 4, 2025
0512a9b
Rollup merge of #136798 - pcorwin:master, r=tgross35
workingjubilee Mar 4, 2025
f21602d
Rollup merge of #136865 - jieyouxu:long-type-path-compare-mode, r=lqd
workingjubilee Mar 4, 2025
a2e328a
Rollup merge of #136975 - jyn514:macos-x, r=Mark-Simulacrum
workingjubilee Mar 4, 2025
a4d9b0e
Rollup merge of #137240 - jieyouxu:remove_dir_all, r=Mark-Simulacrum
workingjubilee Mar 4, 2025
838255b
Rollup merge of #137303 - compiler-errors:maybe-forgor, r=cjgillot
workingjubilee Mar 4, 2025
c5ef305
Rollup merge of #137327 - arlosi:home-dir, r=Mark-Simulacrum
workingjubilee Mar 4, 2025
f841289
Rollup merge of #137502 - compiler-errors:global-asm-aint-mir-body, r…
workingjubilee Mar 4, 2025
af9753a
Rollup merge of #137534 - xizheyin:issue-137342, r=GuillaumeGomez
workingjubilee Mar 4, 2025
25a392f
Rollup merge of #137565 - compiler-errors:macro-ex, r=estebank
workingjubilee Mar 4, 2025
1e4302e
Rollup merge of #137643 - beetrees:repr128-dwarf-variant-test, r=jiey…
workingjubilee Mar 4, 2025
f3a72c2
Rollup merge of #137758 - jdonszelmann:fix-137662, r=nnethercote
workingjubilee Mar 4, 2025
47628e7
Rollup merge of #137764 - compiler-errors:always-applicable-negative-…
workingjubilee Mar 4, 2025
d11595f
Rollup merge of #137772 - thaliaarchi:bstr-display, r=joshtriplett
workingjubilee Mar 4, 2025
2b680c0
Rollup merge of #137798 - marcoieni:ubuntu-24-large-runner-arm, r=Kobzol
workingjubilee Mar 4, 2025
1a19902
Rollup merge of #137805 - RalfJung:layout-debug-print, r=Noratrieb
workingjubilee Mar 4, 2025
bd91bc4
Rollup merge of #137808 - jswrenn:droppy-unsafe-fields, r=nnethercote
workingjubilee Mar 4, 2025
e641ff5
Rollup merge of #137820 - meithecatte:instantiate-opt, r=BoxyUwU
workingjubilee Mar 4, 2025
a547d97
Rollup merge of #137825 - estebank:rtn-sugg-2, r=compiler-errors
workingjubilee Mar 4, 2025
60709ff
Rollup merge of #137829 - cramertj:stabilize-split-off, r=jhpratt
workingjubilee Mar 4, 2025
96ed4ec
Rollup merge of #137834 - lolbinarycat:rustc_fluent_macro-137815, r=B…
workingjubilee Mar 4, 2025
7f2b73b
Rollup merge of #137850 - slanterns:box_uninit_write, r=ibraheemdev
workingjubilee Mar 4, 2025
bd7d00d
Rollup merge of #137912 - compiler-errors:no-missing-lifetime-recover…
workingjubilee Mar 4, 2025
58dd700
Rollup merge of #137913 - compiler-errors:struct-field-default-generi…
workingjubilee Mar 4, 2025
088e780
Rollup merge of #137923 - scottmcm:fix-postorder-size-hint, r=tmiasko
workingjubilee Mar 4, 2025
783e650
Rollup merge of #137949 - jieyouxu:update-install, r=ChrisDenton
workingjubilee Mar 4, 2025
177d721
Rollup merge of #137963 - Eclips4:fix-E0373, r=compiler-errors
workingjubilee Mar 4, 2025
05823f0
Rollup merge of #137975 - Zalathar:needs-hir, r=compiler-errors
workingjubilee Mar 4, 2025
2ac3af8
Rollup merge of #137986 - fuyangpengqi:master, r=Amanieu
workingjubilee Mar 4, 2025
e08afdd
Rollup merge of #137991 - tgross35:avr-book-links, r=jieyouxu
workingjubilee Mar 4, 2025
4e102f8
Rollup merge of #137993 - tmiasko:deduce-comment, r=compiler-errors
workingjubilee Mar 4, 2025
f7b31e9
Rollup merge of #137996 - safinaskar:2025-03-04-revert-parallel, r=co…
workingjubilee Mar 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make ptr_cast_add_auto_to_object lint into hard error
In Rust 1.81, we added a FCW lint (including linting in dependencies)
against pointer casts that add an auto trait to dyn bounds.  This was
part of work making casts of pointers involving trait objects stricter
which was needed to restabilize trait upcasting.

We considered just making this a hard error at the time, but opted
against it due to breakage found by crater.  This breakage was mostly
due to the `anymap` crate which has been a persistent problem for us.

It's now a year later, and the fact that this is not yet a hard error
is giving us pause about stabilizing arbitrary self types and
`derive(CoercePointee)`.  So let's now make a hard error of this.
  • Loading branch information
traviscross committed Feb 22, 2025
commit ef337a6599145766fc73b2896c6ff3e0fef65b0b
41 changes: 41 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0804.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
An auto trait cannot be added to the bounds of a `dyn Trait` type via
a pointer cast.

Erroneous code example:

```rust,edition2021,compile_fail,E0804
let ptr: *const dyn core::any::Any = &();
_ = ptr as *const (dyn core::any::Any + Send);
```

Adding an auto trait can make the vtable invalid, potentially causing
UB in safe code afterwards. For example:

```rust,edition2021,no_run
use core::{mem::transmute, ptr::NonNull};

trait Trait {
fn f(&self)
where
Self: Send;
}

impl Trait for NonNull<()> {
fn f(&self) {
unreachable!()
}
}

fn main() {
let unsend: &dyn Trait = &NonNull::dangling();
let bad: &(dyn Trait + Send) = unsafe { transmute(unsend) };
// This crashes, since the vtable for `NonNull as dyn Trait` does
// not have an entry for `Trait::f`.
bad.f();
}
```

To fix this error, you can use `transmute` rather than pointer casts,
but you must ensure that the vtable is valid for the pointer's type
before calling a method on the trait object or allowing other code to
do so.
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ E0800: 0800,
E0801: 0801,
E0802: 0802,
E0803: 0803,
E0804: 0804,
);
)
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ hir_typeck_pass_to_variadic_function = can't pass `{$ty}` to variadic function
.suggestion = cast the value to `{$cast_ty}`
.teach_help = certain types, like `{$ty}`, must be casted before passing them to a variadic function, because of arcane ABI rules dictated by the C standard

hir_typeck_ptr_cast_add_auto_to_object = adding {$traits_len ->
[1] an auto trait {$traits}
hir_typeck_ptr_cast_add_auto_to_object = cannot add {$traits_len ->
[1] auto trait {$traits}
*[other] auto traits {$traits}
} to a trait object in a pointer cast may cause UB later on
} to dyn bound via pointer cast
.note = this could allow UB elsewhere
.help = use `transmute` if you're sure this is sound
.label = unsupported cast

hir_typeck_remove_semi_for_coerce = you might have meant to return the `match` expression
hir_typeck_remove_semi_for_coerce_expr = this could be implicitly returned but it is a statement, not a tail expression
Expand Down
28 changes: 12 additions & 16 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,23 +936,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
.collect::<Vec<_>>();

if !added.is_empty() {
tcx.emit_node_span_lint(
lint::builtin::PTR_CAST_ADD_AUTO_TO_OBJECT,
self.expr.hir_id,
self.span,
errors::PtrCastAddAutoToObject {
traits_len: added.len(),
traits: {
let mut traits: Vec<_> = added
.into_iter()
.map(|trait_did| tcx.def_path_str(trait_did))
.collect();

traits.sort();
traits.into()
},
tcx.dcx().emit_err(errors::PtrCastAddAutoToObject {
span: self.span,
traits_len: added.len(),
traits: {
let mut traits: Vec<_> = added
.into_iter()
.map(|trait_did| tcx.def_path_str(trait_did))
.collect();

traits.sort();
traits.into()
},
)
});
}

Ok(CastKind::PtrPtrCast)
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,14 @@ pub(crate) struct LossyProvenanceInt2Ptr<'tcx> {
pub sugg: LossyProvenanceInt2PtrSuggestion,
}

#[derive(LintDiagnostic)]
#[diag(hir_typeck_ptr_cast_add_auto_to_object)]
#[derive(Diagnostic)]
#[diag(hir_typeck_ptr_cast_add_auto_to_object, code = E0804)]
#[note]
#[help]
pub(crate) struct PtrCastAddAutoToObject {
#[primary_span]
#[label]
pub span: Span,
pub traits_len: usize,
pub traits: DiagSymbolList<String>,
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ fn register_builtins(store: &mut LintStore) {
"converted into hard error, \
see <https://github.com/rust-lang/rust/issues/73333> for more information",
);
store.register_removed(
"ptr_cast_add_auto_to_object",
"converted into hard error, see issue #127323 \
<https://github.com/rust-lang/rust/issues/127323> for more information",
);
}

fn register_internals(store: &mut LintStore) {
Expand Down
53 changes: 0 additions & 53 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ declare_lint_pass! {
PRIVATE_BOUNDS,
PRIVATE_INTERFACES,
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
PTR_CAST_ADD_AUTO_TO_OBJECT,
PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
REDUNDANT_IMPORTS,
Expand Down Expand Up @@ -4827,58 +4826,6 @@ declare_lint! {
};
}

declare_lint! {
/// The `ptr_cast_add_auto_to_object` lint detects casts of raw pointers to trait
/// objects, which add auto traits.
///
/// ### Example
///
/// ```rust,edition2021,compile_fail
/// let ptr: *const dyn core::any::Any = &();
/// _ = ptr as *const dyn core::any::Any + Send;
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Adding an auto trait can make the vtable invalid, potentially causing
/// UB in safe code afterwards. For example:
///
/// ```ignore (causes a warning)
/// #![feature(arbitrary_self_types)]
///
/// trait Trait {
/// fn f(self: *const Self)
/// where
/// Self: Send;
/// }
///
/// impl Trait for *const () {
/// fn f(self: *const Self) {
/// unreachable!()
/// }
/// }
///
/// fn main() {
/// let unsend: *const () = &();
/// let unsend: *const dyn Trait = &unsend;
/// let send_bad: *const (dyn Trait + Send) = unsend as _;
/// send_bad.f(); // this crashes, since vtable for `*const ()` does not have an entry for `f`
/// }
/// ```
///
/// Generally you must ensure that vtable is right for the pointer's type,
/// before passing the pointer to safe code.
pub PTR_CAST_ADD_AUTO_TO_OBJECT,
Warn,
"detects `as` casts from pointers to `dyn Trait` to pointers to `dyn Trait + Auto`",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
reference: "issue #127323 <https://github.com/rust-lang/rust/issues/127323>",
};
}

declare_lint! {
/// The `out_of_scope_macro_calls` lint detects `macro_rules` called when they are not in scope,
/// above their definition, which may happen in key-value attributes.
Expand Down
14 changes: 8 additions & 6 deletions tests/ui/cast/ptr-to-trait-obj-add-auto.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//@ check-pass

trait Trait<'a> {}

fn add_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send) {
x as _
//~^ warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//~^ ERROR cannot add auto trait `Send` to dyn bound via pointer cast
//~| NOTE unsupported cast
//~| NOTE this could allow UB elsewhere
//~| HELP use `transmute` if you're sure this is sound
}

// (to test diagnostic list formatting)
fn add_multiple_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send + Sync + Unpin) {
x as _
//~^ warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//~^ ERROR cannot add auto traits `Send`, `Sync`, and `Unpin` to dyn bound via pointer cast
//~| NOTE unsupported cast
//~| NOTE this could allow UB elsewhere
//~| HELP use `transmute` if you're sure this is sound
}

fn main() {}
44 changes: 11 additions & 33 deletions tests/ui/cast/ptr-to-trait-obj-add-auto.stderr
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
--> $DIR/ptr-to-trait-obj-add-auto.rs:6:5
error[E0804]: cannot add auto trait `Send` to dyn bound via pointer cast
--> $DIR/ptr-to-trait-obj-add-auto.rs:4:5
|
LL | x as _
| ^^^^^^
| ^^^^^^ unsupported cast
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default
= note: this could allow UB elsewhere
= help: use `transmute` if you're sure this is sound

warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on
error[E0804]: cannot add auto traits `Send`, `Sync`, and `Unpin` to dyn bound via pointer cast
--> $DIR/ptr-to-trait-obj-add-auto.rs:13:5
|
LL | x as _
| ^^^^^^
| ^^^^^^ unsupported cast
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
= note: this could allow UB elsewhere
= help: use `transmute` if you're sure this is sound

warning: 2 warnings emitted

Future incompatibility report: Future breakage diagnostic:
warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on
--> $DIR/ptr-to-trait-obj-add-auto.rs:6:5
|
LL | x as _
| ^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default

Future breakage diagnostic:
warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on
--> $DIR/ptr-to-trait-obj-add-auto.rs:13:5
|
LL | x as _
| ^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323>
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0804`.
4 changes: 4 additions & 0 deletions tests/ui/lint/removed-lints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Removed lints

This directory contains tests to confirm that lints that have been
removed do not cause errors and produce the appropriate warnings.
5 changes: 5 additions & 0 deletions tests/ui/lint/removed-lints/ptr_cast_add_auto_to_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@ check-pass

#![deny(ptr_cast_add_auto_to_object)]
//~^ WARN lint `ptr_cast_add_auto_to_object` has been removed
fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/lint/removed-lints/ptr_cast_add_auto_to_object.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: lint `ptr_cast_add_auto_to_object` has been removed: converted into hard error, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> for more information
--> $DIR/ptr_cast_add_auto_to_object.rs:3:9
|
LL | #![deny(ptr_cast_add_auto_to_object)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(renamed_and_removed_lints)]` on by default

warning: 1 warning emitted