Skip to content

Commit 6053544

Browse files
committed
Check FromIterator trait impl in prelude collision check.
1 parent c5e344f commit 6053544

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ symbols! {
155155
FormatSpec,
156156
Formatter,
157157
From,
158+
FromIterator,
158159
Future,
159160
FxHashMap,
160161
FxHashSet,

compiler/rustc_typeck/src/check/method/prelude2021.rs

+21
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use hir::ItemKind;
44
use rustc_ast::Mutability;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
7+
use rustc_middle::ty::subst::InternalSubsts;
78
use rustc_middle::ty::{Ref, Ty};
89
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
910
use rustc_span::symbol::kw::Underscore;
1011
use rustc_span::symbol::{sym, Ident};
1112
use rustc_span::Span;
13+
use rustc_trait_selection::infer::InferCtxtExt;
1214

1315
use crate::check::{
1416
method::probe::{self, Pick},
@@ -206,6 +208,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
206208
return;
207209
}
208210

211+
// For from_iter, check if the type actualy implements FromIterator.
212+
// If we know it does not, we don't need to warn.
213+
if method_name.name == sym::from_iter {
214+
if let Some(trait_def_id) = self.tcx.get_diagnostic_item(sym::FromIterator) {
215+
if !self
216+
.infcx
217+
.type_implements_trait(
218+
trait_def_id,
219+
self_ty,
220+
InternalSubsts::empty(),
221+
self.param_env,
222+
)
223+
.may_apply()
224+
{
225+
return;
226+
}
227+
}
228+
}
229+
209230
// No need to lint if this is an inherent method called on a specific type, like `Vec::foo(...)`,
210231
// since such methods take precedence over trait methods.
211232
if matches!(pick.kind, probe::PickKind::InherentImplPick) {

library/core/src/iter/traits/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
over elements of type `{A}`",
9090
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
9191
)]
92+
#[rustc_diagnostic_item = "FromIterator"]
9293
pub trait FromIterator<A>: Sized {
9394
/// Creates a value from an iterator.
9495
///

0 commit comments

Comments
 (0)