From 0f468705f483b0b2ec58564ed7bf608858d3b82f Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 2 Jun 2024 12:00:18 +0200 Subject: [PATCH] Add migration lint for 2024 prelude additions This adds the migration lint for the newly ambiguous methods `poll` and `into_future`. When these methods are used on types implementing the respective traits, it will be ambiguous in the future, which can lead to hard errors or behavior changes depending on the exact circumstances. --- compiler/rustc_hir/src/lang_items.rs | 1 + .../src/method/prelude_edition_lints.rs | 22 +++++++++- compiler/rustc_lint_defs/src/builtin.rs | 41 +++++++++++++++++++ compiler/rustc_span/src/symbol.rs | 1 + library/core/src/future/into_future.rs | 1 + .../future-poll-already-future.rs | 17 ++++++++ .../future-poll-async-block.e2021.fixed | 21 ++++++++++ .../future-poll-async-block.e2021.stderr | 16 ++++++++ .../future-poll-async-block.rs | 21 ++++++++++ .../future-poll-not-future.rs | 14 +++++++ .../into-future-adt.e2021.fixed | 29 +++++++++++++ .../into-future-adt.e2021.stderr | 16 ++++++++ .../prelude-migration/into-future-adt.rs | 29 +++++++++++++ .../into-future-already-into-future.rs | 24 +++++++++++ .../into-future-not-into-future.rs | 14 +++++++ 15 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 tests/ui/rust-2024/prelude-migration/future-poll-already-future.rs create mode 100644 tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.fixed create mode 100644 tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.stderr create mode 100644 tests/ui/rust-2024/prelude-migration/future-poll-async-block.rs create mode 100644 tests/ui/rust-2024/prelude-migration/future-poll-not-future.rs create mode 100644 tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.fixed create mode 100644 tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.stderr create mode 100644 tests/ui/rust-2024/prelude-migration/into-future-adt.rs create mode 100644 tests/ui/rust-2024/prelude-migration/into-future-already-into-future.rs create mode 100644 tests/ui/rust-2024/prelude-migration/into-future-not-into-future.rs diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index c3ccba487ede7..9f6b8f7169a4d 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -239,6 +239,7 @@ language_item_table! { Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0); FusedIterator, sym::fused_iterator, fused_iterator_trait, Target::Trait, GenericRequirement::Exact(0); + IntoFuture, sym::into_future_trait, into_future_trait, Target::Trait, GenericRequirement::None; Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0); FutureOutput, sym::future_output, future_output, Target::AssocTy, GenericRequirement::Exact(0); AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0); diff --git a/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs b/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs index 3ee10f74d98a4..cc11e42500476 100644 --- a/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs +++ b/compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs @@ -9,7 +9,7 @@ use rustc_hir as hir; use rustc_lint::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER}; use rustc_middle::span_bug; use rustc_middle::ty::{self, Ty}; -use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS; +use rustc_session::lint::builtin::{RUST_2021_PRELUDE_COLLISIONS, RUST_2024_PRELUDE_COLLISIONS}; use rustc_span::symbol::kw::{Empty, Underscore}; use rustc_span::symbol::{sym, Ident}; use rustc_span::Span; @@ -35,6 +35,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (prelude_or_array_lint, edition) = match segment.ident.name { // `try_into` was added to the prelude in Rust 2021. sym::try_into if !span.at_least_rust_2021() => (RUST_2021_PRELUDE_COLLISIONS, "2021"), + sym::poll + if !span.at_least_rust_2024() + && let Some(future) = self.tcx.lang_items().future_trait() + && self + .infcx + .type_implements_trait(future, [self_ty], self.param_env) + .may_apply() => + { + (RUST_2024_PRELUDE_COLLISIONS, "2024") + } + sym::into_future + if !span.at_least_rust_2024() + && let Some(into_future) = self.tcx.lang_items().into_future_trait() + && self + .infcx + .type_implements_trait(into_future, [self_ty], self.param_env) + .may_apply() => + { + (RUST_2024_PRELUDE_COLLISIONS, "2024") + } // `into_iter` wasn't added to the prelude, // but `[T; N].into_iter()` doesn't resolve to IntoIterator::into_iter // before Rust 2021, which results in the same problem. diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 93995fe60a365..da0864dbdcc8a 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -89,6 +89,7 @@ declare_lint_pass! { RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, RUST_2021_PRELUDE_COLLISIONS, RUST_2024_INCOMPATIBLE_PAT, + RUST_2024_PRELUDE_COLLISIONS, SELF_CONSTRUCTOR_FROM_OUTER_ITEM, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS, SINGLE_USE_LIFETIMES, @@ -3841,6 +3842,46 @@ declare_lint! { }; } +declare_lint! { + /// The `rust_2024_prelude_collisions` lint detects the usage of trait methods which are ambiguous + /// with traits added to the prelude in future editions. + /// + /// ### Example + /// + /// ```rust,edition2021,compile_fail + /// #![deny(rust_2024_prelude_collisions)] + /// trait Meow { + /// fn poll(&self) {} + /// } + /// impl Meow for T {} + /// + /// fn main() { + /// core::pin::pin!(async {}).poll(); + /// // ^^^^^^^^ + /// // This call to try_into matches both Future::poll and Meow::poll as + /// // `Future` has been added to the Rust prelude in 2024 edition. + /// } + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Rust 2024, introduces two new additions to the standard library's prelude, + /// `Future` and `IntoFuture`. This results in an ambiguity as to which method/function + /// to call when an existing `poll`/`into_future` method is called via dot-call syntax or + /// a `poll`/`into_future` associated function is called directly on a type. + /// + pub RUST_2024_PRELUDE_COLLISIONS, + Allow, + "detects the usage of trait methods which are ambiguous with traits added to the \ + prelude in future editions", + @future_incompatible = FutureIncompatibleInfo { + reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024), + reference: "", + }; +} + declare_lint! { /// The `rust_2021_prefixes_incompatible_syntax` lint detects identifiers that will be parsed as a /// prefix instead in Rust 2021. diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 61ca0d54ca490..f4c277efa1e81 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1023,6 +1023,7 @@ symbols! { integral, into_async_iter_into_iter, into_future, + into_future_trait, into_iter, intra_doc_pointers, intrinsics, diff --git a/library/core/src/future/into_future.rs b/library/core/src/future/into_future.rs index eb5a9b72dd0f2..99f1ff554c01a 100644 --- a/library/core/src/future/into_future.rs +++ b/library/core/src/future/into_future.rs @@ -105,6 +105,7 @@ use crate::future::Future; message = "`{Self}` is not a future", note = "{Self} must be a future or must implement `IntoFuture` to be awaited" )] +#[cfg_attr(not(bootstrap), lang = "into_future_trait")] pub trait IntoFuture { /// The output that the future will produce on completion. #[stable(feature = "into_future", since = "1.64.0")] diff --git a/tests/ui/rust-2024/prelude-migration/future-poll-already-future.rs b/tests/ui/rust-2024/prelude-migration/future-poll-already-future.rs new file mode 100644 index 0000000000000..7bf5118c3402e --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/future-poll-already-future.rs @@ -0,0 +1,17 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@ check-pass + +#![deny(rust_2024_prelude_collisions)] + +use std::future::Future; + +fn main() { + core::pin::pin!(async {}).poll(&mut context()); +} + +fn context() -> core::task::Context<'static> { + loop {} +} diff --git a/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.fixed b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.fixed new file mode 100644 index 0000000000000..44850c8c45bbc --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.fixed @@ -0,0 +1,21 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2021] run-rustfix +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@[e2024] check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn poll(&self, _ctx: &mut core::task::Context<'_>) {} +} +impl Meow for T {} +fn main() { + Meow::poll(&core::pin::pin!(async {}), &mut context()); + //[e2021]~^ ERROR trait method `poll` will become ambiguous in Rust 2024 + //[e2021]~| WARN this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! +} + +fn context() -> core::task::Context<'static> { + loop {} +} diff --git a/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.stderr b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.stderr new file mode 100644 index 0000000000000..496b3197c3405 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.e2021.stderr @@ -0,0 +1,16 @@ +error: trait method `poll` will become ambiguous in Rust 2024 + --> $DIR/future-poll-async-block.rs:14:5 + | +LL | core::pin::pin!(async {}).poll(&mut context()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `Meow::poll(&core::pin::pin!(async {}), &mut context())` + | + = warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! + = note: for more information, see +note: the lint level is defined here + --> $DIR/future-poll-async-block.rs:8:9 + | +LL | #![deny(rust_2024_prelude_collisions)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/rust-2024/prelude-migration/future-poll-async-block.rs b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.rs new file mode 100644 index 0000000000000..614e4c786c533 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/future-poll-async-block.rs @@ -0,0 +1,21 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2021] run-rustfix +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@[e2024] check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn poll(&self, _ctx: &mut core::task::Context<'_>) {} +} +impl Meow for T {} +fn main() { + core::pin::pin!(async {}).poll(&mut context()); + //[e2021]~^ ERROR trait method `poll` will become ambiguous in Rust 2024 + //[e2021]~| WARN this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! +} + +fn context() -> core::task::Context<'static> { + loop {} +} diff --git a/tests/ui/rust-2024/prelude-migration/future-poll-not-future.rs b/tests/ui/rust-2024/prelude-migration/future-poll-not-future.rs new file mode 100644 index 0000000000000..21960b54d92b1 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/future-poll-not-future.rs @@ -0,0 +1,14 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@ check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn poll(&self) {} +} +impl Meow for T {} +fn main() { + core::pin::pin!(()).poll(); +} diff --git a/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.fixed b/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.fixed new file mode 100644 index 0000000000000..0b0873eb23842 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.fixed @@ -0,0 +1,29 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2021] run-rustfix +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@[e2024] check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn into_future(&self) {} +} +impl Meow for Cat {} + +struct Cat; + +impl core::future::IntoFuture for Cat { + type Output = (); + type IntoFuture = core::future::Ready<()>; + + fn into_future(self) -> Self::IntoFuture { + core::future::ready(()) + } +} + +fn main() { + Meow::into_future(&Cat); + //[e2021]~^ ERROR trait method `into_future` will become ambiguous in Rust 2024 + //[e2021]~| WARN this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! +} diff --git a/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.stderr b/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.stderr new file mode 100644 index 0000000000000..b74e80e2a4a08 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/into-future-adt.e2021.stderr @@ -0,0 +1,16 @@ +error: trait method `into_future` will become ambiguous in Rust 2024 + --> $DIR/into-future-adt.rs:26:5 + | +LL | Cat.into_future(); + | ^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `Meow::into_future(&Cat)` + | + = warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! + = note: for more information, see +note: the lint level is defined here + --> $DIR/into-future-adt.rs:8:9 + | +LL | #![deny(rust_2024_prelude_collisions)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/rust-2024/prelude-migration/into-future-adt.rs b/tests/ui/rust-2024/prelude-migration/into-future-adt.rs new file mode 100644 index 0000000000000..0db70930bc75b --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/into-future-adt.rs @@ -0,0 +1,29 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2021] run-rustfix +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@[e2024] check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn into_future(&self) {} +} +impl Meow for Cat {} + +struct Cat; + +impl core::future::IntoFuture for Cat { + type Output = (); + type IntoFuture = core::future::Ready<()>; + + fn into_future(self) -> Self::IntoFuture { + core::future::ready(()) + } +} + +fn main() { + Cat.into_future(); + //[e2021]~^ ERROR trait method `into_future` will become ambiguous in Rust 2024 + //[e2021]~| WARN this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024! +} diff --git a/tests/ui/rust-2024/prelude-migration/into-future-already-into-future.rs b/tests/ui/rust-2024/prelude-migration/into-future-already-into-future.rs new file mode 100644 index 0000000000000..6bc2ea317059e --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/into-future-already-into-future.rs @@ -0,0 +1,24 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@ check-pass + +#![deny(rust_2024_prelude_collisions)] + +use core::future::IntoFuture; + +struct Cat; + +impl IntoFuture for Cat { + type Output = (); + type IntoFuture = core::future::Ready<()>; + + fn into_future(self) -> Self::IntoFuture { + core::future::ready(()) + } +} + +fn main() { + let _ = Cat.into_future(); +} diff --git a/tests/ui/rust-2024/prelude-migration/into-future-not-into-future.rs b/tests/ui/rust-2024/prelude-migration/into-future-not-into-future.rs new file mode 100644 index 0000000000000..21960b54d92b1 --- /dev/null +++ b/tests/ui/rust-2024/prelude-migration/into-future-not-into-future.rs @@ -0,0 +1,14 @@ +//@ revisions: e2021 e2024 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 +//@[e2024] compile-flags: -Zunstable-options +//@ check-pass + +#![deny(rust_2024_prelude_collisions)] +trait Meow { + fn poll(&self) {} +} +impl Meow for T {} +fn main() { + core::pin::pin!(()).poll(); +}