Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa3281a

Browse files
authoredSep 5, 2024··
Unrolled build for rust-lang#129664
Rollup merge of rust-lang#129664 - adetaylor:arbitrary-self-types-pointers-feature-gate, r=wesleywiser Arbitrary self types v2: pointers feature gate. The main `arbitrary_self_types` feature gate will shortly be reused for a new version of arbitrary self types which we are amending per [this RFC](https://github.com/rust-lang/rfcs/blob/master/text/3519-arbitrary-self-types-v2.md). The main amendments are: * _do_ support `self` types which can't safely implement `Deref` * do _not_ support generic `self` types * do _not_ support raw pointers as `self` types. This PR relates to the last of those bullet points: this strips pointer support from the current `arbitrary_self_types` feature. We expect this to cause some amount of breakage for crates using this unstable feature to allow raw pointer self types. If that's the case, we want to know about it, and we want crate authors to know of the upcoming changes. For now, this can be resolved by adding the new `arbitrary_self_types_pointers` feature to such crates. If we determine that use of raw pointers as self types is common, then we may maintain that as an unstable feature even if we come to stabilize the rest of the `arbitrary_self_types` support in future. If we don't hear that this PR is causing breakage, then perhaps we don't need it at all, even behind an unstable feature gate. [Tracking issue](rust-lang#44874) This is [step 4 of the plan outlined here](rust-lang#44874 (comment))
2 parents 009e738 + 8e20b66 commit aa3281a

14 files changed

+132
-34
lines changed
 

‎compiler/rustc_feature/src/unstable.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@ declare_features! (
349349
(unstable, adt_const_params, "1.56.0", Some(95174)),
350350
/// Allows defining an `#[alloc_error_handler]`.
351351
(unstable, alloc_error_handler, "1.29.0", Some(51540)),
352-
/// Allows trait methods with arbitrary self types.
352+
/// Allows inherent and trait methods with arbitrary self types.
353353
(unstable, arbitrary_self_types, "1.23.0", Some(44874)),
354+
/// Allows inherent and trait methods with arbitrary self types that are raw pointers.
355+
(unstable, arbitrary_self_types_pointers, "CURRENT_RUSTC_VERSION", Some(44874)),
354356
/// Enables experimental inline assembly support for additional architectures.
355357
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
356358
/// Allows using `label` operands in inline assembly.

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs

+63-19
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,13 @@ fn check_fn_or_method<'tcx>(
16521652
}
16531653
}
16541654

1655+
/// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.
1656+
#[derive(Clone, Copy, PartialEq)]
1657+
enum ArbitrarySelfTypesLevel {
1658+
Basic, // just arbitrary_self_types
1659+
WithPointers, // both arbitrary_self_types and arbitrary_self_types_pointers
1660+
}
1661+
16551662
#[instrument(level = "debug", skip(wfcx))]
16561663
fn check_method_receiver<'tcx>(
16571664
wfcx: &WfCheckingCtxt<'_, 'tcx>,
@@ -1684,40 +1691,77 @@ fn check_method_receiver<'tcx>(
16841691
return Ok(());
16851692
}
16861693

1687-
if tcx.features().arbitrary_self_types {
1688-
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
1689-
// Report error; `arbitrary_self_types` was enabled.
1690-
return Err(tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty }));
1691-
}
1694+
let arbitrary_self_types_level = if tcx.features().arbitrary_self_types_pointers {
1695+
Some(ArbitrarySelfTypesLevel::WithPointers)
1696+
} else if tcx.features().arbitrary_self_types {
1697+
Some(ArbitrarySelfTypesLevel::Basic)
16921698
} else {
1693-
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) {
1694-
return Err(if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
1699+
None
1700+
};
1701+
1702+
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level) {
1703+
return Err(match arbitrary_self_types_level {
1704+
// Wherever possible, emit a message advising folks that the features
1705+
// `arbitrary_self_types` or `arbitrary_self_types_pointers` might
1706+
// have helped.
1707+
None if receiver_is_valid(
1708+
wfcx,
1709+
span,
1710+
receiver_ty,
1711+
self_ty,
1712+
Some(ArbitrarySelfTypesLevel::Basic),
1713+
) =>
1714+
{
16951715
// Report error; would have worked with `arbitrary_self_types`.
16961716
feature_err(
16971717
&tcx.sess,
16981718
sym::arbitrary_self_types,
16991719
span,
17001720
format!(
17011721
"`{receiver_ty}` cannot be used as the type of `self` without \
1702-
the `arbitrary_self_types` feature",
1722+
the `arbitrary_self_types` feature",
17031723
),
17041724
)
17051725
.with_help(fluent::hir_analysis_invalid_receiver_ty_help)
17061726
.emit()
1707-
} else {
1708-
// Report error; would not have worked with `arbitrary_self_types`.
1727+
}
1728+
None | Some(ArbitrarySelfTypesLevel::Basic)
1729+
if receiver_is_valid(
1730+
wfcx,
1731+
span,
1732+
receiver_ty,
1733+
self_ty,
1734+
Some(ArbitrarySelfTypesLevel::WithPointers),
1735+
) =>
1736+
{
1737+
// Report error; would have worked with `arbitrary_self_types_pointers`.
1738+
feature_err(
1739+
&tcx.sess,
1740+
sym::arbitrary_self_types_pointers,
1741+
span,
1742+
format!(
1743+
"`{receiver_ty}` cannot be used as the type of `self` without \
1744+
the `arbitrary_self_types_pointers` feature",
1745+
),
1746+
)
1747+
.with_help(fluent::hir_analysis_invalid_receiver_ty_help)
1748+
.emit()
1749+
}
1750+
_ =>
1751+
// Report error; would not have worked with `arbitrary_self_types[_pointers]`.
1752+
{
17091753
tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty })
1710-
});
1711-
}
1754+
}
1755+
});
17121756
}
17131757
Ok(())
17141758
}
17151759

17161760
/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
17171761
/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
1718-
/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more
1719-
/// strict: `receiver_ty` must implement `Receiver` and directly implement
1720-
/// `Deref<Target = self_ty>`.
1762+
/// through a `*const/mut T` raw pointer if `arbitrary_self_types_pointers` is also enabled.
1763+
/// If neither feature is enabled, the requirements are more strict: `receiver_ty` must implement
1764+
/// `Receiver` and directly implement `Deref<Target = self_ty>`.
17211765
///
17221766
/// N.B., there are cases this function returns `true` but causes an error to be emitted,
17231767
/// particularly when `receiver_ty` derefs to a type that is the same as `self_ty` but has the
@@ -1727,7 +1771,7 @@ fn receiver_is_valid<'tcx>(
17271771
span: Span,
17281772
receiver_ty: Ty<'tcx>,
17291773
self_ty: Ty<'tcx>,
1730-
arbitrary_self_types_enabled: bool,
1774+
arbitrary_self_types_enabled: Option<ArbitrarySelfTypesLevel>,
17311775
) -> bool {
17321776
let infcx = wfcx.infcx;
17331777
let tcx = wfcx.tcx();
@@ -1745,8 +1789,8 @@ fn receiver_is_valid<'tcx>(
17451789

17461790
let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_def_id, span, receiver_ty);
17471791

1748-
// The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
1749-
if arbitrary_self_types_enabled {
1792+
// The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
1793+
if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) {
17501794
autoderef = autoderef.include_raw_pointers();
17511795
}
17521796

@@ -1772,7 +1816,7 @@ fn receiver_is_valid<'tcx>(
17721816

17731817
// Without `feature(arbitrary_self_types)`, we require that each step in the
17741818
// deref chain implement `receiver`.
1775-
if !arbitrary_self_types_enabled {
1819+
if arbitrary_self_types_enabled.is_none() {
17761820
if !receiver_is_implemented(
17771821
wfcx,
17781822
receiver_trait_def_id,

‎compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
403403
mode,
404404
}));
405405
} else if bad_ty.reached_raw_pointer
406-
&& !self.tcx.features().arbitrary_self_types
406+
&& !self.tcx.features().arbitrary_self_types_pointers
407407
&& !self.tcx.sess.at_least_rust_2018()
408408
{
409409
// this case used to be allowed by the compiler,

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ symbols! {
407407
append_const_msg,
408408
arbitrary_enum_discriminant,
409409
arbitrary_self_types,
410+
arbitrary_self_types_pointers,
410411
args,
411412
arith_offset,
412413
arm,

‎src/tools/miri/tests/pass/dyn-arbitrary-self.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)]
3+
#![feature(arbitrary_self_types_pointers, unsize, coerce_unsized, dispatch_from_dyn)]
44
#![feature(rustc_attrs)]
55

66
fn pin_box_dyn() {

‎tests/mir-opt/building/receiver_ptr_mutability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// skip-filecheck
22
// EMIT_MIR receiver_ptr_mutability.main.built.after.mir
33

4-
#![feature(arbitrary_self_types)]
4+
#![feature(arbitrary_self_types_pointers)]
55

66
struct Test {}
77

‎tests/ui/cast/ptr-to-trait-obj-different-regions-lt-ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// issue: <https://github.com/rust-lang/rust/issues/120217>
44

5-
#![feature(arbitrary_self_types)]
5+
#![feature(arbitrary_self_types_pointers)]
66

77
trait Static<'a> {
88
fn proof(self: *const Self, s: &'a str) -> &'static str;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Foo {
2+
fn foo(self: *const Self); //~ ERROR `*const Self` cannot be used as the type of `self`
3+
}
4+
5+
struct Bar;
6+
7+
impl Foo for Bar {
8+
fn foo(self: *const Self) {} //~ ERROR `*const Bar` cannot be used as the type of `self`
9+
}
10+
11+
impl Bar {
12+
fn bar(self: *mut Self) {} //~ ERROR `*mut Bar` cannot be used as the type of `self`
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0658]: `*const Bar` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
2+
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:8:18
3+
|
4+
LL | fn foo(self: *const Self) {}
5+
| ^^^^^^^^^^^
6+
|
7+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
8+
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
11+
12+
error[E0658]: `*mut Bar` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
13+
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:12:18
14+
|
15+
LL | fn bar(self: *mut Self) {}
16+
| ^^^^^^^^^
17+
|
18+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
19+
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
20+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
21+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
22+
23+
error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
24+
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:2:18
25+
|
26+
LL | fn foo(self: *const Self);
27+
| ^^^^^^^^^^^
28+
|
29+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
30+
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
31+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
32+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0658`.

‎tests/ui/feature-gates/feature-gate-arbitrary_self_types-raw-pointer.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types` feature
1+
error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
22
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:4:18
33
|
44
LL | fn foo(self: *const Self) {}
55
| ^^^^^^^^^^^
66
|
77
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
8-
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
8+
= help: add `#![feature(arbitrary_self_types_pointers)]` 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
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
1111

12-
error[E0658]: `*const ()` cannot be used as the type of `self` without the `arbitrary_self_types` feature
12+
error[E0658]: `*const ()` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
1313
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:14:18
1414
|
1515
LL | fn bar(self: *const Self) {}
1616
| ^^^^^^^^^^^
1717
|
1818
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
19-
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
19+
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
2020
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2121
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
2222

23-
error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature
23+
error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
2424
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:9:18
2525
|
2626
LL | fn bar(self: *const Self);
2727
| ^^^^^^^^^^^
2828
|
2929
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
30-
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
30+
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
3131
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3232
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
3333

‎tests/ui/inference/auxiliary/inference_unstable_iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(staged_api)]
2-
#![feature(arbitrary_self_types)]
2+
#![feature(arbitrary_self_types_pointers)]
33

44
#![stable(feature = "ipu_iterator", since = "1.0.0")]
55

‎tests/ui/inference/auxiliary/inference_unstable_itertools.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(arbitrary_self_types)]
1+
#![feature(arbitrary_self_types_pointers)]
22

33
pub trait IpuItertools {
44
fn ipu_flatten(&self) -> u32 {

‎tests/ui/self/arbitrary_self_types_raw_pointer_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-pass
2-
#![feature(arbitrary_self_types)]
2+
#![feature(arbitrary_self_types_pointers)]
33

44
use std::rc::Rc;
55

‎tests/ui/self/arbitrary_self_types_raw_pointer_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-pass
2-
#![feature(arbitrary_self_types)]
2+
#![feature(arbitrary_self_types_pointers)]
33

44
use std::ptr;
55

0 commit comments

Comments
 (0)
Please sign in to comment.