Auto merge of #85707 - jam1garner:future_prelude_collision_lint, r=nikomatsakis
Add `future_prelude_collision` lint Implements #84594. (RFC rust-lang/rfcs#3114 ([rendered](https://github.com/rust-lang/rfcs/blob/master/text/3114-prelude-2021.md))) Not entirely complete but wanted to have my progress decently available while I finish off the last little bits. Things left to implement: * [x] UI tests for lints * [x] Only emit lint for 2015 and 2018 editions * [ ] Lint name/message bikeshedding * [x] Implement for `FromIterator` (from best I can tell, the current approach as mentioned from [this comment](https://github.com/rust-lang/rust/issues/84594#issuecomment-847288288) won't work due to `FromIterator` instances not using dot-call syntax, but if I'm correct about this then that would also need to be fixed for `TryFrom`/`TryInto`)* * [x] Add to `rust-2021-migration` group? (See #85512) (added to `rust-2021-compatibility` group) * [ ] Link to edition guide in lint docs *edit: looked into it, `lookup_method` will also not be hit for `TryFrom`/`TryInto` for non-dotcall syntax. If anyone who is more familiar with typecheck knows the equivalent for looking up associated functions, feel free to chime in.
This commit is contained in:
commit
44f4a87d70
22 changed files with 1144 additions and 31 deletions
|
@ -3001,6 +3001,7 @@ declare_lint_pass! {
|
|||
PROC_MACRO_BACK_COMPAT,
|
||||
OR_PATTERNS_BACK_COMPAT,
|
||||
LARGE_ASSIGNMENTS,
|
||||
FUTURE_PRELUDE_COLLISION,
|
||||
]
|
||||
}
|
||||
|
||||
|
@ -3244,3 +3245,52 @@ declare_lint! {
|
|||
edition: Some(Edition::Edition2021),
|
||||
};
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
/// The `future_prelude_collision` lint detects the usage of trait methods which are ambiguous
|
||||
/// with traits added to the prelude in future editions.
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// #![deny(future_prelude_collision)]
|
||||
///
|
||||
/// trait Foo {
|
||||
/// fn try_into(self) -> Result<String, !>;
|
||||
/// }
|
||||
///
|
||||
/// impl Foo for &str {
|
||||
/// fn try_into(self) -> Result<String, !> {
|
||||
/// Ok(String::from(self))
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let x: String = "3".try_into().unwrap();
|
||||
/// // ^^^^^^^^
|
||||
/// // This call to try_into matches both Foo:try_into and TryInto::try_into as
|
||||
/// // `TryInto` has been added to the Rust prelude in 2021 edition.
|
||||
/// println!("{}", x);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// {{produces}}
|
||||
///
|
||||
/// ### Explanation
|
||||
///
|
||||
/// In Rust 2021, one of the important introductions is the [prelude changes], which add
|
||||
/// `TryFrom`, `TryInto`, and `FromIterator` into the standard library's prelude. Since this
|
||||
/// results in an amiguity as to which method/function to call when an existing `try_into`
|
||||
/// method is called via dot-call syntax or a `try_from`/`from_iter` associated function
|
||||
/// is called directly on a type.
|
||||
///
|
||||
/// [prelude changes]: https://blog.rust-lang.org/inside-rust/2021/03/04/planning-rust-2021.html#prelude-changes
|
||||
pub FUTURE_PRELUDE_COLLISION,
|
||||
Allow,
|
||||
"detects the usage of trait methods which are ambiguous with traits added to the \
|
||||
prelude in future editions",
|
||||
@future_incompatible = FutureIncompatibleInfo {
|
||||
reference: "issue #85684 <https://github.com/rust-lang/rust/issues/85684>",
|
||||
edition: Some(Edition::Edition2021),
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue