Skip to content

Commit

Permalink
Unrolled build for rust-lang#129664
Browse files Browse the repository at this point in the history
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))
  • Loading branch information
rust-timer committed Sep 5, 2024
2 parents 009e738 + 8e20b66 commit aa3281a
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 34 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ declare_features! (
(unstable, adt_const_params, "1.56.0", Some(95174)),
/// Allows defining an `#[alloc_error_handler]`.
(unstable, alloc_error_handler, "1.29.0", Some(51540)),
/// Allows trait methods with arbitrary self types.
/// Allows inherent and trait methods with arbitrary self types.
(unstable, arbitrary_self_types, "1.23.0", Some(44874)),
/// Allows inherent and trait methods with arbitrary self types that are raw pointers.
(unstable, arbitrary_self_types_pointers, "CURRENT_RUSTC_VERSION", Some(44874)),
/// Enables experimental inline assembly support for additional architectures.
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
/// Allows using `label` operands in inline assembly.
Expand Down
82 changes: 63 additions & 19 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,13 @@ fn check_fn_or_method<'tcx>(
}
}

/// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`.
#[derive(Clone, Copy, PartialEq)]
enum ArbitrarySelfTypesLevel {
Basic, // just arbitrary_self_types
WithPointers, // both arbitrary_self_types and arbitrary_self_types_pointers
}

#[instrument(level = "debug", skip(wfcx))]
fn check_method_receiver<'tcx>(
wfcx: &WfCheckingCtxt<'_, 'tcx>,
Expand Down Expand Up @@ -1684,40 +1691,77 @@ fn check_method_receiver<'tcx>(
return Ok(());
}

if tcx.features().arbitrary_self_types {
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
// Report error; `arbitrary_self_types` was enabled.
return Err(tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty }));
}
let arbitrary_self_types_level = if tcx.features().arbitrary_self_types_pointers {
Some(ArbitrarySelfTypesLevel::WithPointers)
} else if tcx.features().arbitrary_self_types {
Some(ArbitrarySelfTypesLevel::Basic)
} else {
if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) {
return Err(if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) {
None
};

if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level) {
return Err(match arbitrary_self_types_level {
// Wherever possible, emit a message advising folks that the features
// `arbitrary_self_types` or `arbitrary_self_types_pointers` might
// have helped.
None if receiver_is_valid(
wfcx,
span,
receiver_ty,
self_ty,
Some(ArbitrarySelfTypesLevel::Basic),
) =>
{
// Report error; would have worked with `arbitrary_self_types`.
feature_err(
&tcx.sess,
sym::arbitrary_self_types,
span,
format!(
"`{receiver_ty}` cannot be used as the type of `self` without \
the `arbitrary_self_types` feature",
the `arbitrary_self_types` feature",
),
)
.with_help(fluent::hir_analysis_invalid_receiver_ty_help)
.emit()
} else {
// Report error; would not have worked with `arbitrary_self_types`.
}
None | Some(ArbitrarySelfTypesLevel::Basic)
if receiver_is_valid(
wfcx,
span,
receiver_ty,
self_ty,
Some(ArbitrarySelfTypesLevel::WithPointers),
) =>
{
// Report error; would have worked with `arbitrary_self_types_pointers`.
feature_err(
&tcx.sess,
sym::arbitrary_self_types_pointers,
span,
format!(
"`{receiver_ty}` cannot be used as the type of `self` without \
the `arbitrary_self_types_pointers` feature",
),
)
.with_help(fluent::hir_analysis_invalid_receiver_ty_help)
.emit()
}
_ =>
// Report error; would not have worked with `arbitrary_self_types[_pointers]`.
{
tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty })
});
}
}
});
}
Ok(())
}

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

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

// The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled {
// The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) {
autoderef = autoderef.include_raw_pointers();
}

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

// Without `feature(arbitrary_self_types)`, we require that each step in the
// deref chain implement `receiver`.
if !arbitrary_self_types_enabled {
if arbitrary_self_types_enabled.is_none() {
if !receiver_is_implemented(
wfcx,
receiver_trait_def_id,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
mode,
}));
} else if bad_ty.reached_raw_pointer
&& !self.tcx.features().arbitrary_self_types
&& !self.tcx.features().arbitrary_self_types_pointers
&& !self.tcx.sess.at_least_rust_2018()
{
// this case used to be allowed by the compiler,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ symbols! {
append_const_msg,
arbitrary_enum_discriminant,
arbitrary_self_types,
arbitrary_self_types_pointers,
args,
arith_offset,
arm,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/dyn-arbitrary-self.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@revisions: stack tree
//@[tree]compile-flags: -Zmiri-tree-borrows
#![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)]
#![feature(arbitrary_self_types_pointers, unsize, coerce_unsized, dispatch_from_dyn)]
#![feature(rustc_attrs)]

fn pin_box_dyn() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/building/receiver_ptr_mutability.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// skip-filecheck
// EMIT_MIR receiver_ptr_mutability.main.built.after.mir

#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

struct Test {}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/cast/ptr-to-trait-obj-different-regions-lt-ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// issue: <https://github.com/rust-lang/rust/issues/120217>

#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

trait Static<'a> {
fn proof(self: *const Self, s: &'a str) -> &'static str;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait Foo {
fn foo(self: *const Self); //~ ERROR `*const Self` cannot be used as the type of `self`
}

struct Bar;

impl Foo for Bar {
fn foo(self: *const Self) {} //~ ERROR `*const Bar` cannot be used as the type of `self`
}

impl Bar {
fn bar(self: *mut Self) {} //~ ERROR `*mut Bar` cannot be used as the type of `self`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0658]: `*const Bar` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:8:18
|
LL | fn foo(self: *const Self) {}
| ^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

error[E0658]: `*mut Bar` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:12:18
|
LL | fn bar(self: *mut Self) {}
| ^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary-self-types-pointers.rs:2:18
|
LL | fn foo(self: *const Self);
| ^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types` feature
error[E0658]: `*const Foo` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:4:18
|
LL | fn foo(self: *const Self) {}
| ^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

error[E0658]: `*const ()` cannot be used as the type of `self` without the `arbitrary_self_types` feature
error[E0658]: `*const ()` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:14:18
|
LL | fn bar(self: *const Self) {}
| ^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types` feature
error[E0658]: `*const Self` cannot be used as the type of `self` without the `arbitrary_self_types_pointers` feature
--> $DIR/feature-gate-arbitrary_self_types-raw-pointer.rs:9:18
|
LL | fn bar(self: *const Self);
| ^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
= help: add `#![feature(arbitrary_self_types_pointers)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= 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`)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(staged_api)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

pub trait IpuItertools {
fn ipu_flatten(&self) -> u32 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/self/arbitrary_self_types_raw_pointer_struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ run-pass
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

use std::rc::Rc;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/self/arbitrary_self_types_raw_pointer_trait.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ run-pass
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

use std::ptr;

Expand Down

0 comments on commit aa3281a

Please sign in to comment.