1use std::borrow::Cow;
4use std::iter;
5use std::ops::Deref;
6
7use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt, Visitor, walk_ty};
8use rustc_ast::{
9 self as ast, AngleBracketedArg, AssocItemKind, DUMMY_NODE_ID, Expr, ExprKind, GenericArg,
10 GenericArgs, GenericParam, GenericParamKind, Item, ItemKind, MethodCall, NodeId, Path,
11 PathSegment, Ty, TyKind,
12};
13use rustc_ast_pretty::pprust::{path_to_string, where_bound_predicate_to_string};
14use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
15use rustc_data_structures::unord::UnordItems;
16use rustc_errors::codes::*;
17use rustc_errors::{
18 Applicability, Diag, Diagnostic, ErrorGuaranteed, MultiSpan, SuggestionStyle, pluralize,
19 struct_span_code_err,
20};
21use rustc_hir as hir;
22use rustc_hir::def::Namespace::{self, *};
23use rustc_hir::def::{CtorKind, CtorOf, DefKind, MacroKinds};
24use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
25use rustc_hir::{MissingLifetimeKind, PrimTy, find_attr};
26use rustc_middle::ty;
27use rustc_session::{Session, lint};
28use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
29use rustc_span::edition::Edition;
30use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
31use thin_vec::ThinVec;
32use tracing::debug;
33
34use super::NoConstantGenericsReason;
35use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
36use crate::late::{
37 AliasPossibility, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind,
38 LifetimeUseSet, QSelf, RibKind,
39};
40use crate::ty::fast_reject::SimplifiedType;
41use crate::{
42 Finalize, Module, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res, Resolver,
43 ScopeSet, Segment, errors, path_names_to_string,
44};
45
46enum AssocSuggestion {
48 Field(Span),
49 MethodWithSelf { called: bool },
50 AssocFn { called: bool },
51 AssocType,
52 AssocConst,
53}
54
55impl AssocSuggestion {
56 fn action(&self) -> &'static str {
57 match self {
58 AssocSuggestion::Field(_) => "use the available field",
59 AssocSuggestion::MethodWithSelf { called: true } => {
60 "call the method with the fully-qualified path"
61 }
62 AssocSuggestion::MethodWithSelf { called: false } => {
63 "refer to the method with the fully-qualified path"
64 }
65 AssocSuggestion::AssocFn { called: true } => "call the associated function",
66 AssocSuggestion::AssocFn { called: false } => "refer to the associated function",
67 AssocSuggestion::AssocConst => "use the associated `const`",
68 AssocSuggestion::AssocType => "use the associated type",
69 }
70 }
71}
72
73fn is_self_type(path: &[Segment], namespace: Namespace) -> bool {
74 namespace == TypeNS && path.len() == 1 && path[0].ident.name == kw::SelfUpper
75}
76
77fn is_self_value(path: &[Segment], namespace: Namespace) -> bool {
78 namespace == ValueNS && path.len() == 1 && path[0].ident.name == kw::SelfLower
79}
80
81fn path_to_string_without_assoc_item_bindings(path: &Path) -> String {
82 let mut path = path.clone();
83 for segment in &mut path.segments {
84 let mut remove_args = false;
85 if let Some(args) = segment.args.as_deref_mut()
86 && let ast::GenericArgs::AngleBracketed(angle_bracketed) = args
87 {
88 angle_bracketed.args.retain(|arg| #[allow(non_exhaustive_omitted_patterns)] match arg {
ast::AngleBracketedArg::Arg(_) => true,
_ => false,
}matches!(arg, ast::AngleBracketedArg::Arg(_)));
89 remove_args = angle_bracketed.args.is_empty();
90 }
91 if remove_args {
92 segment.args = None;
93 }
94 }
95 path_to_string(&path)
96}
97
98fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, String) {
100 let variant_path = &suggestion.path;
101 let variant_path_string = path_names_to_string(variant_path);
102
103 let path_len = suggestion.path.segments.len();
104 let enum_path = ast::Path {
105 span: suggestion.path.span,
106 segments: suggestion.path.segments[0..path_len - 1].iter().cloned().collect(),
107 tokens: None,
108 };
109 let enum_path_string = path_names_to_string(&enum_path);
110
111 (variant_path_string, enum_path_string)
112}
113
114#[derive(#[automatically_derived]
impl ::core::marker::Copy for MissingLifetime { }Copy, #[automatically_derived]
impl ::core::clone::Clone for MissingLifetime {
#[inline]
fn clone(&self) -> MissingLifetime {
let _: ::core::clone::AssertParamIsClone<NodeId>;
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<MissingLifetimeKind>;
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for MissingLifetime {
#[inline]
fn eq(&self, other: &MissingLifetime) -> bool {
self.id == other.id && self.id_for_lint == other.id_for_lint &&
self.span == other.span && self.kind == other.kind &&
self.count == other.count
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for MissingLifetime {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<NodeId>;
let _: ::core::cmp::AssertParamIsEq<Span>;
let _: ::core::cmp::AssertParamIsEq<MissingLifetimeKind>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for MissingLifetime {
#[inline]
fn partial_cmp(&self, other: &MissingLifetime)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for MissingLifetime {
#[inline]
fn cmp(&self, other: &MissingLifetime) -> ::core::cmp::Ordering {
match ::core::cmp::Ord::cmp(&self.id, &other.id) {
::core::cmp::Ordering::Equal =>
match ::core::cmp::Ord::cmp(&self.id_for_lint,
&other.id_for_lint) {
::core::cmp::Ordering::Equal =>
match ::core::cmp::Ord::cmp(&self.span, &other.span) {
::core::cmp::Ordering::Equal =>
match ::core::cmp::Ord::cmp(&self.kind, &other.kind) {
::core::cmp::Ordering::Equal =>
::core::cmp::Ord::cmp(&self.count, &other.count),
cmp => cmp,
},
cmp => cmp,
},
cmp => cmp,
},
cmp => cmp,
}
}
}Ord, #[automatically_derived]
impl ::core::fmt::Debug for MissingLifetime {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f,
"MissingLifetime", "id", &self.id, "id_for_lint",
&self.id_for_lint, "span", &self.span, "kind", &self.kind,
"count", &&self.count)
}
}Debug)]
116pub(super) struct MissingLifetime {
117 pub id: NodeId,
119 pub id_for_lint: NodeId,
126 pub span: Span,
128 pub kind: MissingLifetimeKind,
130 pub count: usize,
132}
133
134#[derive(#[automatically_derived]
impl ::core::clone::Clone for ElisionFnParameter {
#[inline]
fn clone(&self) -> ElisionFnParameter {
ElisionFnParameter {
index: ::core::clone::Clone::clone(&self.index),
ident: ::core::clone::Clone::clone(&self.ident),
lifetime_count: ::core::clone::Clone::clone(&self.lifetime_count),
span: ::core::clone::Clone::clone(&self.span),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ElisionFnParameter {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"ElisionFnParameter", "index", &self.index, "ident", &self.ident,
"lifetime_count", &self.lifetime_count, "span", &&self.span)
}
}Debug)]
137pub(super) struct ElisionFnParameter {
138 pub index: usize,
140 pub ident: Option<Ident>,
142 pub lifetime_count: usize,
144 pub span: Span,
146}
147
148#[derive(#[automatically_derived]
impl ::core::clone::Clone for LifetimeElisionCandidate {
#[inline]
fn clone(&self) -> LifetimeElisionCandidate {
let _: ::core::clone::AssertParamIsClone<MissingLifetime>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LifetimeElisionCandidate { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for LifetimeElisionCandidate {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LifetimeElisionCandidate::Ignore =>
::core::fmt::Formatter::write_str(f, "Ignore"),
LifetimeElisionCandidate::Missing(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Missing", &__self_0),
}
}
}Debug)]
151pub(super) enum LifetimeElisionCandidate {
152 Ignore,
154 Missing(MissingLifetime),
155}
156
157#[derive(#[automatically_derived]
impl ::core::fmt::Debug for BaseError {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["msg", "fallback_label", "span", "span_label", "could_be_expr",
"suggestion", "module"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.msg, &self.fallback_label, &self.span, &self.span_label,
&self.could_be_expr, &self.suggestion, &&self.module];
::core::fmt::Formatter::debug_struct_fields_finish(f, "BaseError",
names, values)
}
}Debug)]
159struct BaseError {
160 msg: String,
161 fallback_label: String,
162 span: Span,
163 span_label: Option<(Span, &'static str)>,
164 could_be_expr: bool,
165 suggestion: Option<(Span, &'static str, String)>,
166 module: Option<DefId>,
167}
168
169#[derive(#[automatically_derived]
impl ::core::fmt::Debug for TypoCandidate {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TypoCandidate::Typo(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Typo",
&__self_0),
TypoCandidate::Shadowed(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Shadowed", __self_0, &__self_1),
TypoCandidate::None =>
::core::fmt::Formatter::write_str(f, "None"),
}
}
}Debug)]
170enum TypoCandidate {
171 Typo(TypoSuggestion),
172 Shadowed(Res, Option<Span>),
173 None,
174}
175
176impl TypoCandidate {
177 fn to_opt_suggestion(self) -> Option<TypoSuggestion> {
178 match self {
179 TypoCandidate::Typo(sugg) => Some(sugg),
180 TypoCandidate::Shadowed(_, _) | TypoCandidate::None => None,
181 }
182 }
183}
184
185impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
186 fn trait_assoc_type_def_id_by_name(
187 &mut self,
188 trait_def_id: DefId,
189 assoc_name: Symbol,
190 ) -> Option<DefId> {
191 let module = self.r.get_module(trait_def_id)?;
192 self.r.resolutions(module).borrow().iter().find_map(|(key, resolution)| {
193 if key.ident.name != assoc_name {
194 return None;
195 }
196 let resolution = resolution.borrow();
197 let binding = resolution.best_decl()?;
198 match binding.res() {
199 Res::Def(DefKind::AssocTy, def_id) => Some(def_id),
200 _ => None,
201 }
202 })
203 }
204
205 fn suggest_assoc_type_from_bounds(
207 &mut self,
208 err: &mut Diag<'_>,
209 source: PathSource<'_, 'ast, 'ra>,
210 path: &[Segment],
211 ident_span: Span,
212 ) -> bool {
213 if source.namespace() != TypeNS {
215 return false;
216 }
217 let [segment] = path else { return false };
218 if segment.has_generic_args {
219 return false;
220 }
221 if !ident_span.can_be_used_for_suggestions() {
222 return false;
223 }
224 let assoc_name = segment.ident.name;
225 if assoc_name == kw::Underscore {
226 return false;
227 }
228
229 let mut matching_bounds: FxIndexMap<
233 Symbol,
234 FxIndexMap<DefId, (DefId, FxIndexSet<String>)>,
235 > = FxIndexMap::default();
236
237 let mut record_bound = |this: &mut Self,
238 ty_param: Symbol,
239 poly_trait_ref: &ast::PolyTraitRef| {
240 if !poly_trait_ref.bound_generic_params.is_empty() {
242 return;
243 }
244 if poly_trait_ref.modifiers != ast::TraitBoundModifiers::NONE {
245 return;
246 }
247 let Some(trait_seg) = poly_trait_ref.trait_ref.path.segments.last() else {
248 return;
249 };
250 let Some(partial_res) = this.r.partial_res_map.get(&trait_seg.id) else {
251 return;
252 };
253 let Some(trait_def_id) = partial_res.full_res().and_then(|res| res.opt_def_id()) else {
254 return;
255 };
256 let Some(assoc_type_def_id) =
257 this.trait_assoc_type_def_id_by_name(trait_def_id, assoc_name)
258 else {
259 return;
260 };
261
262 let trait_path =
266 path_to_string_without_assoc_item_bindings(&poly_trait_ref.trait_ref.path);
267 let trait_bounds = matching_bounds.entry(ty_param).or_default();
268 let trait_bounds = trait_bounds
269 .entry(trait_def_id)
270 .or_insert_with(|| (assoc_type_def_id, FxIndexSet::default()));
271 if true {
match (&trait_bounds.0, &assoc_type_def_id) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(trait_bounds.0, assoc_type_def_id);
272 trait_bounds.1.insert(trait_path);
273 };
274
275 let mut record_from_generics = |this: &mut Self, generics: &ast::Generics| {
276 for param in &generics.params {
277 let ast::GenericParamKind::Type { .. } = param.kind else { continue };
278 for bound in ¶m.bounds {
279 let ast::GenericBound::Trait(poly_trait_ref) = bound else { continue };
280 record_bound(this, param.ident.name, poly_trait_ref);
281 }
282 }
283
284 for predicate in &generics.where_clause.predicates {
285 let ast::WherePredicateKind::BoundPredicate(where_bound) = &predicate.kind else {
286 continue;
287 };
288
289 let ast::TyKind::Path(None, bounded_path) = &where_bound.bounded_ty.kind else {
290 continue;
291 };
292 let [ast::PathSegment { ident, args: None, .. }] = &bounded_path.segments[..]
293 else {
294 continue;
295 };
296
297 let Some(partial_res) = this.r.partial_res_map.get(&where_bound.bounded_ty.id)
299 else {
300 continue;
301 };
302 if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
Some(Res::Def(DefKind::TyParam, _)) => true,
_ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::TyParam, _))) {
303 continue;
304 }
305
306 for bound in &where_bound.bounds {
307 let ast::GenericBound::Trait(poly_trait_ref) = bound else { continue };
308 record_bound(this, ident.name, poly_trait_ref);
309 }
310 }
311 };
312
313 if let Some(item) = self.diag_metadata.current_item
314 && let Some(generics) = item.kind.generics()
315 {
316 record_from_generics(self, generics);
317 }
318
319 if let Some(item) = self.diag_metadata.current_item
320 && #[allow(non_exhaustive_omitted_patterns)] match item.kind {
ItemKind::Impl(..) => true,
_ => false,
}matches!(item.kind, ItemKind::Impl(..))
321 && let Some(assoc) = self.diag_metadata.current_impl_item
322 {
323 let generics = match &assoc.kind {
324 AssocItemKind::Const(ast::ConstItem { generics, .. })
325 | AssocItemKind::Fn(ast::Fn { generics, .. })
326 | AssocItemKind::Type(ast::TyAlias { generics, .. }) => Some(generics),
327 AssocItemKind::Delegation(..)
328 | AssocItemKind::MacCall(..)
329 | AssocItemKind::DelegationMac(..) => None,
330 };
331 if let Some(generics) = generics {
332 record_from_generics(self, generics);
333 }
334 }
335
336 let mut suggestions: FxIndexSet<String> = FxIndexSet::default();
337 for (ty_param, traits) in matching_bounds {
338 let ty_param = ty_param.to_ident_string();
339 let trait_paths_len: usize = traits.values().map(|(_, paths)| paths.len()).sum();
340 if traits.len() == 1 && trait_paths_len == 1 {
341 let assoc_type_def_id = traits.values().next().unwrap().0;
342 let assoc_segment = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", assoc_name,
self.r.item_required_generic_args_suggestion(assoc_type_def_id)))
})format!(
343 "{}{}",
344 assoc_name,
345 self.r.item_required_generic_args_suggestion(assoc_type_def_id)
346 );
347 suggestions.insert(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}", ty_param,
assoc_segment))
})format!("{ty_param}::{assoc_segment}"));
348 } else {
349 for (assoc_type_def_id, trait_paths) in traits.into_values() {
350 let assoc_segment = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", assoc_name,
self.r.item_required_generic_args_suggestion(assoc_type_def_id)))
})format!(
351 "{}{}",
352 assoc_name,
353 self.r.item_required_generic_args_suggestion(assoc_type_def_id)
354 );
355 for trait_path in trait_paths {
356 suggestions
357 .insert(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0} as {1}>::{2}", ty_param,
trait_path, assoc_segment))
})format!("<{ty_param} as {trait_path}>::{assoc_segment}"));
358 }
359 }
360 }
361 }
362
363 if suggestions.is_empty() {
364 return false;
365 }
366
367 let mut suggestions: Vec<String> = suggestions.into_iter().collect();
368 suggestions.sort();
369
370 err.span_suggestions_with_style(
371 ident_span,
372 "you might have meant to use an associated type of the same name",
373 suggestions,
374 Applicability::MaybeIncorrect,
375 SuggestionStyle::ShowAlways,
376 );
377
378 true
379 }
380
381 fn make_base_error(
382 &mut self,
383 path: &[Segment],
384 span: Span,
385 source: PathSource<'_, 'ast, 'ra>,
386 res: Option<Res>,
387 ) -> BaseError {
388 let mut expected = source.descr_expected();
390 let path_str = Segment::names_to_string(path);
391 let item_str = path.last().unwrap().ident;
392
393 if let Some(res) = res {
394 BaseError {
395 msg: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}, found {1} `{2}`",
expected, res.descr(), path_str))
})format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
396 fallback_label: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("not a {0}", expected))
})format!("not a {expected}"),
397 span,
398 span_label: match res {
399 Res::Def(DefKind::TyParam, def_id) => {
400 Some((self.r.def_span(def_id), "found this type parameter"))
401 }
402 _ => None,
403 },
404 could_be_expr: match res {
405 Res::Def(DefKind::Fn, _) => {
406 self.r
408 .tcx
409 .sess
410 .source_map()
411 .span_to_snippet(span)
412 .is_ok_and(|snippet| snippet.ends_with(')'))
413 }
414 Res::Def(
415 DefKind::Ctor(..)
416 | DefKind::AssocFn
417 | DefKind::Const { .. }
418 | DefKind::AssocConst { .. },
419 _,
420 )
421 | Res::SelfCtor(_)
422 | Res::PrimTy(_)
423 | Res::Local(_) => true,
424 _ => false,
425 },
426 suggestion: None,
427 module: None,
428 }
429 } else {
430 let mut span_label = None;
431 let item_ident = path.last().unwrap().ident;
432 let item_span = item_ident.span;
433 let (mod_prefix, mod_str, module, suggestion) = if path.len() == 1 {
434 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:434",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(434u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["self.diag_metadata.current_impl_items"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&self.diag_metadata.current_impl_items)
as &dyn Value))])
});
} else { ; }
};debug!(?self.diag_metadata.current_impl_items);
435 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:435",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(435u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["self.diag_metadata.current_function"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&self.diag_metadata.current_function)
as &dyn Value))])
});
} else { ; }
};debug!(?self.diag_metadata.current_function);
436 let suggestion = if self.current_trait_ref.is_none()
437 && let Some((fn_kind, _)) = self.diag_metadata.current_function
438 && let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
439 && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = fn_kind
440 && let Some(items) = self.diag_metadata.current_impl_items
441 && let Some(item) = items.iter().find(|i| {
442 i.kind.ident().is_some_and(|ident| {
443 ident.name == item_str.name && !sig.span.contains(item_span)
445 })
446 }) {
447 let sp = item_span.shrink_to_lo();
448
449 let field = match source {
452 PathSource::Expr(Some(Expr { kind: ExprKind::Struct(expr), .. })) => {
453 expr.fields.iter().find(|f| f.ident == item_ident)
454 }
455 _ => None,
456 };
457 let pre = if let Some(field) = field
458 && field.is_shorthand
459 {
460 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: ", item_ident))
})format!("{item_ident}: ")
461 } else {
462 String::new()
463 };
464 let is_call = match field {
467 Some(ast::ExprField { expr, .. }) => {
468 #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
ExprKind::Call(..) => true,
_ => false,
}matches!(expr.kind, ExprKind::Call(..))
469 }
470 _ => #[allow(non_exhaustive_omitted_patterns)] match source {
PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), .. })) => true,
_ => false,
}matches!(
471 source,
472 PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), .. })),
473 ),
474 };
475
476 match &item.kind {
477 AssocItemKind::Fn(fn_)
478 if (!sig.decl.has_self() || !is_call) && fn_.sig.decl.has_self() =>
479 {
480 span_label = Some((
484 fn_.ident.span,
485 "a method by that name is available on `Self` here",
486 ));
487 None
488 }
489 AssocItemKind::Fn(fn_) if !fn_.sig.decl.has_self() && !is_call => {
490 span_label = Some((
491 fn_.ident.span,
492 "an associated function by that name is available on `Self` here",
493 ));
494 None
495 }
496 AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => {
497 Some((sp, "consider using the method on `Self`", ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}self.", pre))
})format!("{pre}self.")))
498 }
499 AssocItemKind::Fn(_) => Some((
500 sp,
501 "consider using the associated function on `Self`",
502 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}Self::", pre))
})format!("{pre}Self::"),
503 )),
504 AssocItemKind::Const(..) => Some((
505 sp,
506 "consider using the associated constant on `Self`",
507 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}Self::", pre))
})format!("{pre}Self::"),
508 )),
509 _ => None,
510 }
511 } else {
512 None
513 };
514 (String::new(), "this scope".to_string(), None, suggestion)
515 } else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
516 if self.r.tcx.sess.edition() > Edition::Edition2015 {
517 expected = "crate";
520 (String::new(), "the list of imported crates".to_string(), None, None)
521 } else {
522 (
523 String::new(),
524 "the crate root".to_string(),
525 Some(CRATE_DEF_ID.to_def_id()),
526 None,
527 )
528 }
529 } else if path.len() == 2 && path[0].ident.name == kw::Crate {
530 (String::new(), "the crate root".to_string(), Some(CRATE_DEF_ID.to_def_id()), None)
531 } else {
532 let mod_path = &path[..path.len() - 1];
533 let mod_res = self.resolve_path(mod_path, Some(TypeNS), None, source);
534 let mod_prefix = match mod_res {
535 PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
536 _ => None,
537 };
538
539 let module_did = mod_prefix.as_ref().and_then(Res::mod_def_id);
540
541 let mod_prefix =
542 mod_prefix.map_or_else(String::new, |res| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ", res.descr()))
})format!("{} ", res.descr()));
543 (mod_prefix, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`",
Segment::names_to_string(mod_path)))
})format!("`{}`", Segment::names_to_string(mod_path)), module_did, None)
544 };
545
546 let (fallback_label, suggestion) = if path_str == "async"
547 && expected.starts_with("struct")
548 {
549 ("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
550 } else {
551 let override_suggestion =
553 if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) {
554 let item_typo = item_str.to_string().to_lowercase();
555 Some((item_span, "you may want to use a bool value instead", item_typo))
556 } else if item_str.as_str() == "printf" {
559 Some((
560 item_span,
561 "you may have meant to use the `print` macro",
562 "print!".to_owned(),
563 ))
564 } else {
565 suggestion
566 };
567 (::alloc::__export::must_use({
::alloc::fmt::format(format_args!("not found in {0}", mod_str))
})format!("not found in {mod_str}"), override_suggestion)
568 };
569
570 BaseError {
571 msg: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot find {0} `{1}` in {2}{3}",
expected, item_str, mod_prefix, mod_str))
})format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
572 fallback_label,
573 span: item_span,
574 span_label,
575 could_be_expr: false,
576 suggestion,
577 module,
578 }
579 }
580 }
581
582 pub(crate) fn smart_resolve_partial_mod_path_errors(
590 &mut self,
591 prefix_path: &[Segment],
592 following_seg: Option<&Segment>,
593 ) -> Vec<ImportSuggestion> {
594 if let Some(segment) = prefix_path.last()
595 && let Some(following_seg) = following_seg
596 {
597 let candidates = self.r.lookup_import_candidates(
598 segment.ident,
599 Namespace::TypeNS,
600 &self.parent_scope,
601 &|res: Res| #[allow(non_exhaustive_omitted_patterns)] match res {
Res::Def(DefKind::Mod, _) => true,
_ => false,
}matches!(res, Res::Def(DefKind::Mod, _)),
602 );
603 candidates
605 .into_iter()
606 .filter(|candidate| {
607 if let Some(def_id) = candidate.did
608 && let Some(module) = self.r.get_module(def_id)
609 {
610 Some(def_id) != self.parent_scope.module.opt_def_id()
611 && self
612 .r
613 .resolutions(module)
614 .borrow()
615 .iter()
616 .any(|(key, _r)| key.ident.name == following_seg.ident.name)
617 } else {
618 false
619 }
620 })
621 .collect::<Vec<_>>()
622 } else {
623 Vec::new()
624 }
625 }
626
627 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("smart_resolve_report_errors",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(629u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["path",
"following_seg", "span", "source", "res", "qself"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&path)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&following_seg)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&res)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&qself)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
(Diag<'tcx>, Vec<ImportSuggestion>) = loop {};
return __tracing_attr_fake_return;
}
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:639",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(639u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["res", "source"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&res) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&source) as
&dyn Value))])
});
} else { ; }
};
let base_error = self.make_base_error(path, span, source, res);
let code = source.error_code(res.is_some());
let mut err =
self.r.dcx().struct_span_err(base_error.span,
base_error.msg.clone());
err.code(code);
if let Some(within_macro_span) =
base_error.span.within_macro(span,
self.r.tcx.sess.source_map()) {
err.span_label(within_macro_span,
"due to this macro variable");
}
self.detect_missing_binding_available_from_pattern(&mut err, path,
following_seg);
self.suggest_at_operator_in_slice_pat_with_range(&mut err, path);
self.suggest_range_struct_destructuring(&mut err, path, source);
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err,
source, res, base_error.span);
if let Some((span, label)) = base_error.span_label {
err.span_label(span, label);
}
if let Some(ref sugg) = base_error.suggestion {
err.span_suggestion_verbose(sugg.0, sugg.1, &sugg.2,
Applicability::MaybeIncorrect);
}
self.suggest_changing_type_to_const_param(&mut err, res, source,
path, following_seg, span);
self.explain_functions_in_pattern(&mut err, res, source);
if self.suggest_pattern_match_with_let(&mut err, source, span) {
err.span_label(base_error.span, base_error.fallback_label);
return (err, Vec::new());
}
self.suggest_self_or_self_ref(&mut err, path, span);
self.detect_assoc_type_constraint_meant_as_path(&mut err,
&base_error);
self.detect_rtn_with_fully_qualified_path(&mut err, path,
following_seg, span, source, res, qself);
if self.suggest_self_ty(&mut err, source, path, span) ||
self.suggest_self_value(&mut err, source, path, span) {
return (err, Vec::new());
}
if let Some((did, item)) =
self.lookup_doc_alias_name(path, source.namespace()) {
let item_name = item.name;
let suggestion_name = self.r.tcx.item_name(did);
err.span_suggestion(item.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` has a name defined in the doc alias attribute as `{1}`",
suggestion_name, item_name))
}), suggestion_name, Applicability::MaybeIncorrect);
return (err, Vec::new());
};
let (found, suggested_candidates, mut candidates) =
self.try_lookup_name_relaxed(&mut err, source, path,
following_seg, span, res, &base_error);
if found { return (err, candidates); }
if self.suggest_shadowed(&mut err, source, path, following_seg,
span) {
candidates.clear();
}
let mut fallback =
self.suggest_trait_and_bounds(&mut err, source, res, span,
&base_error);
fallback |=
self.suggest_typo(&mut err, source, path, following_seg, span,
&base_error, suggested_candidates);
if fallback {
err.span_label(base_error.span, base_error.fallback_label);
}
self.err_code_special_cases(&mut err, source, path, span);
let module =
base_error.module.unwrap_or_else(|| CRATE_DEF_ID.to_def_id());
self.r.find_cfg_stripped(&mut err,
&path.last().unwrap().ident.name, module);
(err, candidates)
}
}
}#[tracing::instrument(skip(self), level = "debug")]
630 pub(crate) fn smart_resolve_report_errors(
631 &mut self,
632 path: &[Segment],
633 following_seg: Option<&Segment>,
634 span: Span,
635 source: PathSource<'_, 'ast, 'ra>,
636 res: Option<Res>,
637 qself: Option<&QSelf>,
638 ) -> (Diag<'tcx>, Vec<ImportSuggestion>) {
639 debug!(?res, ?source);
640 let base_error = self.make_base_error(path, span, source, res);
641
642 let code = source.error_code(res.is_some());
643 let mut err = self.r.dcx().struct_span_err(base_error.span, base_error.msg.clone());
644 err.code(code);
645
646 if let Some(within_macro_span) =
649 base_error.span.within_macro(span, self.r.tcx.sess.source_map())
650 {
651 err.span_label(within_macro_span, "due to this macro variable");
652 }
653
654 self.detect_missing_binding_available_from_pattern(&mut err, path, following_seg);
655 self.suggest_at_operator_in_slice_pat_with_range(&mut err, path);
656 self.suggest_range_struct_destructuring(&mut err, path, source);
657 self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
658
659 if let Some((span, label)) = base_error.span_label {
660 err.span_label(span, label);
661 }
662
663 if let Some(ref sugg) = base_error.suggestion {
664 err.span_suggestion_verbose(sugg.0, sugg.1, &sugg.2, Applicability::MaybeIncorrect);
665 }
666
667 self.suggest_changing_type_to_const_param(&mut err, res, source, path, following_seg, span);
668 self.explain_functions_in_pattern(&mut err, res, source);
669
670 if self.suggest_pattern_match_with_let(&mut err, source, span) {
671 err.span_label(base_error.span, base_error.fallback_label);
673 return (err, Vec::new());
674 }
675
676 self.suggest_self_or_self_ref(&mut err, path, span);
677 self.detect_assoc_type_constraint_meant_as_path(&mut err, &base_error);
678 self.detect_rtn_with_fully_qualified_path(
679 &mut err,
680 path,
681 following_seg,
682 span,
683 source,
684 res,
685 qself,
686 );
687 if self.suggest_self_ty(&mut err, source, path, span)
688 || self.suggest_self_value(&mut err, source, path, span)
689 {
690 return (err, Vec::new());
691 }
692
693 if let Some((did, item)) = self.lookup_doc_alias_name(path, source.namespace()) {
694 let item_name = item.name;
695 let suggestion_name = self.r.tcx.item_name(did);
696 err.span_suggestion(
697 item.span,
698 format!("`{suggestion_name}` has a name defined in the doc alias attribute as `{item_name}`"),
699 suggestion_name,
700 Applicability::MaybeIncorrect
701 );
702
703 return (err, Vec::new());
704 };
705
706 let (found, suggested_candidates, mut candidates) = self.try_lookup_name_relaxed(
707 &mut err,
708 source,
709 path,
710 following_seg,
711 span,
712 res,
713 &base_error,
714 );
715 if found {
716 return (err, candidates);
717 }
718
719 if self.suggest_shadowed(&mut err, source, path, following_seg, span) {
720 candidates.clear();
722 }
723
724 let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
725 fallback |= self.suggest_typo(
726 &mut err,
727 source,
728 path,
729 following_seg,
730 span,
731 &base_error,
732 suggested_candidates,
733 );
734
735 if fallback {
736 err.span_label(base_error.span, base_error.fallback_label);
738 }
739 self.err_code_special_cases(&mut err, source, path, span);
740
741 let module = base_error.module.unwrap_or_else(|| CRATE_DEF_ID.to_def_id());
742 self.r.find_cfg_stripped(&mut err, &path.last().unwrap().ident.name, module);
743
744 (err, candidates)
745 }
746
747 fn detect_rtn_with_fully_qualified_path(
748 &self,
749 err: &mut Diag<'_>,
750 path: &[Segment],
751 following_seg: Option<&Segment>,
752 span: Span,
753 source: PathSource<'_, '_, '_>,
754 res: Option<Res>,
755 qself: Option<&QSelf>,
756 ) {
757 if let Some(Res::Def(DefKind::AssocFn, _)) = res
758 && let PathSource::TraitItem(TypeNS, _) = source
759 && let None = following_seg
760 && let Some(qself) = qself
761 && let TyKind::Path(None, ty_path) = &qself.ty.kind
762 && ty_path.segments.len() == 1
763 && self.diag_metadata.current_where_predicate.is_some()
764 {
765 err.span_suggestion_verbose(
766 span,
767 "you might have meant to use the return type notation syntax",
768 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::{1}(..)",
ty_path.segments[0].ident, path[path.len() - 1].ident))
})format!("{}::{}(..)", ty_path.segments[0].ident, path[path.len() - 1].ident),
769 Applicability::MaybeIncorrect,
770 );
771 }
772 }
773
774 fn detect_assoc_type_constraint_meant_as_path(
775 &self,
776 err: &mut Diag<'_>,
777 base_error: &BaseError,
778 ) {
779 let Some(ty) = self.diag_metadata.current_type_path else {
780 return;
781 };
782 let TyKind::Path(_, path) = &ty.kind else {
783 return;
784 };
785 for segment in &path.segments {
786 let Some(params) = &segment.args else {
787 continue;
788 };
789 let ast::GenericArgs::AngleBracketed(params) = params.deref() else {
790 continue;
791 };
792 for param in ¶ms.args {
793 let ast::AngleBracketedArg::Constraint(constraint) = param else {
794 continue;
795 };
796 let ast::AssocItemConstraintKind::Bound { bounds } = &constraint.kind else {
797 continue;
798 };
799 for bound in bounds {
800 let ast::GenericBound::Trait(trait_ref) = bound else {
801 continue;
802 };
803 if trait_ref.modifiers == ast::TraitBoundModifiers::NONE
804 && base_error.span == trait_ref.span
805 {
806 err.span_suggestion_verbose(
807 constraint.ident.span.between(trait_ref.span),
808 "you might have meant to write a path instead of an associated type bound",
809 "::",
810 Applicability::MachineApplicable,
811 );
812 }
813 }
814 }
815 }
816 }
817
818 fn suggest_self_or_self_ref(&mut self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
819 if !self.self_type_is_available() {
820 return;
821 }
822 let Some(path_last_segment) = path.last() else { return };
823 let item_str = path_last_segment.ident;
824 if ["this", "my"].contains(&item_str.as_str()) {
826 err.span_suggestion_short(
827 span,
828 "you might have meant to use `self` here instead",
829 "self",
830 Applicability::MaybeIncorrect,
831 );
832 if !self.self_value_is_available(path[0].ident.span) {
833 if let Some((FnKind::Fn(_, _, ast::Fn { sig, .. }), fn_span)) =
834 &self.diag_metadata.current_function
835 {
836 let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
837 (param.span.shrink_to_lo(), "&self, ")
838 } else {
839 (
840 self.r
841 .tcx
842 .sess
843 .source_map()
844 .span_through_char(*fn_span, '(')
845 .shrink_to_hi(),
846 "&self",
847 )
848 };
849 err.span_suggestion_verbose(
850 span,
851 "if you meant to use `self`, you are also missing a `self` receiver \
852 argument",
853 sugg,
854 Applicability::MaybeIncorrect,
855 );
856 }
857 }
858 }
859 }
860
861 fn try_lookup_name_relaxed(
862 &mut self,
863 err: &mut Diag<'_>,
864 source: PathSource<'_, '_, '_>,
865 path: &[Segment],
866 following_seg: Option<&Segment>,
867 span: Span,
868 res: Option<Res>,
869 base_error: &BaseError,
870 ) -> (bool, FxHashSet<String>, Vec<ImportSuggestion>) {
871 let span = match following_seg {
872 Some(_) if path[0].ident.span.eq_ctxt(path[path.len() - 1].ident.span) => {
873 path[0].ident.span.to(path[path.len() - 1].ident.span)
876 }
877 _ => span,
878 };
879 let mut suggested_candidates = FxHashSet::default();
880 let ident = path.last().unwrap().ident;
882 let is_expected = &|res| source.is_expected(res);
883 let ns = source.namespace();
884 let is_enum_variant = &|res| #[allow(non_exhaustive_omitted_patterns)] match res {
Res::Def(DefKind::Variant, _) => true,
_ => false,
}matches!(res, Res::Def(DefKind::Variant, _));
885 let path_str = Segment::names_to_string(path);
886 let ident_span = path.last().map_or(span, |ident| ident.ident.span);
887 let mut candidates = self
888 .r
889 .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
890 .into_iter()
891 .filter(|ImportSuggestion { did, .. }| {
892 match (did, res.and_then(|res| res.opt_def_id())) {
893 (Some(suggestion_did), Some(actual_did)) => *suggestion_did != actual_did,
894 _ => true,
895 }
896 })
897 .collect::<Vec<_>>();
898 let intrinsic_candidates: Vec<_> = candidates
901 .extract_if(.., |sugg| {
902 let path = path_names_to_string(&sugg.path);
903 path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::")
904 })
905 .collect();
906 if candidates.is_empty() {
907 candidates = intrinsic_candidates;
909 }
910 let crate_def_id = CRATE_DEF_ID.to_def_id();
911 if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
912 let mut enum_candidates: Vec<_> = self
913 .r
914 .lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant)
915 .into_iter()
916 .map(|suggestion| import_candidate_to_enum_paths(&suggestion))
917 .filter(|(_, enum_ty_path)| !enum_ty_path.starts_with("std::prelude::"))
918 .collect();
919 if !enum_candidates.is_empty() {
920 enum_candidates.sort();
921
922 let preamble = if res.is_none() {
925 let others = match enum_candidates.len() {
926 1 => String::new(),
927 2 => " and 1 other".to_owned(),
928 n => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" and {0} others", n))
})format!(" and {n} others"),
929 };
930 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there is an enum variant `{0}`{1}; ",
enum_candidates[0].0, others))
})format!("there is an enum variant `{}`{}; ", enum_candidates[0].0, others)
931 } else {
932 String::new()
933 };
934 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}try using the variant\'s enum",
preamble))
})format!("{preamble}try using the variant's enum");
935
936 suggested_candidates.extend(
937 enum_candidates
938 .iter()
939 .map(|(_variant_path, enum_ty_path)| enum_ty_path.clone()),
940 );
941 err.span_suggestions(
942 span,
943 msg,
944 enum_candidates.into_iter().map(|(_variant_path, enum_ty_path)| enum_ty_path),
945 Applicability::MachineApplicable,
946 );
947 }
948 }
949
950 let typo_sugg = self
952 .lookup_typo_candidate(path, following_seg, source.namespace(), is_expected)
953 .to_opt_suggestion()
954 .filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()));
955 if let [segment] = path
956 && !#[allow(non_exhaustive_omitted_patterns)] match source {
PathSource::Delegation => true,
_ => false,
}matches!(source, PathSource::Delegation)
957 && self.self_type_is_available()
958 {
959 if let Some(candidate) =
960 self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
961 {
962 let self_is_available = self.self_value_is_available(segment.ident.span);
963 let pre = match source {
966 PathSource::Expr(Some(Expr { kind: ExprKind::Struct(expr), .. }))
967 if expr
968 .fields
969 .iter()
970 .any(|f| f.ident == segment.ident && f.is_shorthand) =>
971 {
972 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: ", path_str))
})format!("{path_str}: ")
973 }
974 _ => String::new(),
975 };
976 match candidate {
977 AssocSuggestion::Field(field_span) => {
978 if self_is_available {
979 let source_map = self.r.tcx.sess.source_map();
980 let field_is_format_named_arg = #[allow(non_exhaustive_omitted_patterns)] match span.desugaring_kind() {
Some(DesugaringKind::FormatLiteral { .. }) => true,
_ => false,
}matches!(
981 span.desugaring_kind(),
982 Some(DesugaringKind::FormatLiteral { .. })
983 ) && source_map
984 .span_to_source(span, |s, start, _| {
985 Ok(s.get(start.saturating_sub(1)..start) == Some("{"))
986 })
987 .unwrap_or(false);
988 if field_is_format_named_arg {
989 err.help(
990 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use the available field in a format string: `\"{{}}\", self.{0}`",
segment.ident.name))
})format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
991 );
992 } else {
993 err.span_suggestion_verbose(
994 span.shrink_to_lo(),
995 "you might have meant to use the available field",
996 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}self.", pre))
})format!("{pre}self."),
997 Applicability::MaybeIncorrect,
998 );
999 }
1000 } else {
1001 err.span_label(field_span, "a field by that name exists in `Self`");
1002 }
1003 }
1004 AssocSuggestion::MethodWithSelf { called } if self_is_available => {
1005 let msg = if called {
1006 "you might have meant to call the method"
1007 } else {
1008 "you might have meant to refer to the method"
1009 };
1010 err.span_suggestion_verbose(
1011 span.shrink_to_lo(),
1012 msg,
1013 "self.",
1014 Applicability::MachineApplicable,
1015 );
1016 }
1017 AssocSuggestion::MethodWithSelf { .. }
1018 | AssocSuggestion::AssocFn { .. }
1019 | AssocSuggestion::AssocConst
1020 | AssocSuggestion::AssocType => {
1021 err.span_suggestion_verbose(
1022 span.shrink_to_lo(),
1023 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to {0}",
candidate.action()))
})format!("you might have meant to {}", candidate.action()),
1024 "Self::",
1025 Applicability::MachineApplicable,
1026 );
1027 }
1028 }
1029 self.r.add_typo_suggestion(err, typo_sugg, ident_span);
1030 return (true, suggested_candidates, candidates);
1031 }
1032
1033 if let Some((call_span, args_span)) = self.call_has_self_arg(source) {
1035 let mut args_snippet = String::new();
1036 if let Some(args_span) = args_span
1037 && let Ok(snippet) = self.r.tcx.sess.source_map().span_to_snippet(args_span)
1038 {
1039 args_snippet = snippet;
1040 }
1041
1042 if let Some(Res::Def(DefKind::Struct, def_id)) = res {
1043 if let Some(ctor) = self.r.struct_ctor(def_id)
1044 && ctor.has_private_fields(self.parent_scope.module, self.r)
1045 {
1046 if #[allow(non_exhaustive_omitted_patterns)] match ctor.res {
Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _) => true,
_ => false,
}matches!(
1047 ctor.res,
1048 Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _)
1049 ) {
1050 self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
1051 }
1052 err.note("constructor is not visible here due to private fields");
1053 }
1054 } else {
1055 err.span_suggestion(
1056 call_span,
1057 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try calling `{0}` as a method",
ident))
})format!("try calling `{ident}` as a method"),
1058 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("self.{0}({1})", path_str,
args_snippet))
})format!("self.{path_str}({args_snippet})"),
1059 Applicability::MachineApplicable,
1060 );
1061 }
1062
1063 return (true, suggested_candidates, candidates);
1064 }
1065 }
1066
1067 if let Some(res) = res {
1069 if self.smart_resolve_context_dependent_help(
1070 err,
1071 span,
1072 source,
1073 path,
1074 res,
1075 &path_str,
1076 &base_error.fallback_label,
1077 ) {
1078 self.r.add_typo_suggestion(err, typo_sugg, ident_span);
1080 return (true, suggested_candidates, candidates);
1081 }
1082 }
1083
1084 if let Some(rib) = &self.last_block_rib {
1086 for (ident, &res) in &rib.bindings {
1087 if let Res::Local(_) = res
1088 && path.len() == 1
1089 && ident.span.eq_ctxt(path[0].ident.span)
1090 && ident.name == path[0].ident.name
1091 {
1092 err.span_help(
1093 ident.span,
1094 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the binding `{0}` is available in a different scope in the same function",
path_str))
})format!("the binding `{path_str}` is available in a different scope in the same function"),
1095 );
1096 return (true, suggested_candidates, candidates);
1097 }
1098 }
1099 }
1100
1101 if candidates.is_empty() {
1102 candidates = self.smart_resolve_partial_mod_path_errors(path, following_seg);
1103 }
1104
1105 (false, suggested_candidates, candidates)
1106 }
1107
1108 fn lookup_doc_alias_name(&mut self, path: &[Segment], ns: Namespace) -> Option<(DefId, Ident)> {
1109 let find_doc_alias_name = |r: &mut Resolver<'ra, '_>, m: Module<'ra>, item_name: Symbol| {
1110 for resolution in r.resolutions(m).borrow().values() {
1111 let Some(did) =
1112 resolution.borrow().best_decl().and_then(|binding| binding.res().opt_def_id())
1113 else {
1114 continue;
1115 };
1116 if did.is_local() {
1117 continue;
1121 }
1122 if let Some(d) = {
{
'done:
{
for i in ::rustc_hir::attrs::HasAttrs::get_attrs(did, &r.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(Doc(d)) => {
break 'done Some(d);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}hir::find_attr!(r.tcx, did, Doc(d) => d)
1123 && d.aliases.contains_key(&item_name)
1124 {
1125 return Some(did);
1126 }
1127 }
1128 None
1129 };
1130
1131 if path.len() == 1 {
1132 for rib in self.ribs[ns].iter().rev() {
1133 let item = path[0].ident;
1134 if let RibKind::Module(module) | RibKind::Block(Some(module)) = rib.kind
1135 && let Some(did) = find_doc_alias_name(self.r, module.to_module(), item.name)
1136 {
1137 return Some((did, item));
1138 }
1139 }
1140 } else {
1141 for (idx, seg) in path.iter().enumerate().rev().skip(1) {
1150 let Some(id) = seg.id else {
1151 continue;
1152 };
1153 let Some(res) = self.r.partial_res_map.get(&id) else {
1154 continue;
1155 };
1156 if let Res::Def(DefKind::Mod, module) = res.expect_full_res()
1157 && let module = self.r.expect_module(module)
1158 && let item = path[idx + 1].ident
1159 && let Some(did) = find_doc_alias_name(self.r, module, item.name)
1160 {
1161 return Some((did, item));
1162 }
1163 break;
1164 }
1165 }
1166 None
1167 }
1168
1169 fn suggest_trait_and_bounds(
1170 &self,
1171 err: &mut Diag<'_>,
1172 source: PathSource<'_, '_, '_>,
1173 res: Option<Res>,
1174 span: Span,
1175 base_error: &BaseError,
1176 ) -> bool {
1177 let is_macro =
1178 base_error.span.from_expansion() && base_error.span.desugaring_kind().is_none();
1179 let mut fallback = false;
1180
1181 if let (
1182 PathSource::Trait(AliasPossibility::Maybe),
1183 Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
1184 false,
1185 ) = (source, res, is_macro)
1186 && let Some(bounds @ [first_bound, .., last_bound]) =
1187 self.diag_metadata.current_trait_object
1188 {
1189 fallback = true;
1190 let spans: Vec<Span> = bounds
1191 .iter()
1192 .map(|bound| bound.span())
1193 .filter(|&sp| sp != base_error.span)
1194 .collect();
1195
1196 let start_span = first_bound.span();
1197 let end_span = last_bound.span();
1199 let last_bound_span = spans.last().cloned().unwrap();
1201 let mut multi_span: MultiSpan = spans.clone().into();
1202 for sp in spans {
1203 let msg = if sp == last_bound_span {
1204 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("...because of {0} bound{1}",
if bounds.len() - 1 == 1 { "this" } else { "these" },
if bounds.len() - 1 == 1 { "" } else { "s" }))
})format!(
1205 "...because of {these} bound{s}",
1206 these = pluralize!("this", bounds.len() - 1),
1207 s = pluralize!(bounds.len() - 1),
1208 )
1209 } else {
1210 String::new()
1211 };
1212 multi_span.push_span_label(sp, msg);
1213 }
1214 multi_span.push_span_label(base_error.span, "expected this type to be a trait...");
1215 err.span_help(
1216 multi_span,
1217 "`+` is used to constrain a \"trait object\" type with lifetimes or \
1218 auto-traits; structs and enums can't be bound in that way",
1219 );
1220 if bounds.iter().all(|bound| match bound {
1221 ast::GenericBound::Outlives(_) | ast::GenericBound::Use(..) => true,
1222 ast::GenericBound::Trait(tr) => tr.span == base_error.span,
1223 }) {
1224 let mut sugg = ::alloc::vec::Vec::new()vec![];
1225 if base_error.span != start_span {
1226 sugg.push((start_span.until(base_error.span), String::new()));
1227 }
1228 if base_error.span != end_span {
1229 sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
1230 }
1231
1232 err.multipart_suggestion(
1233 "if you meant to use a type and not a trait here, remove the bounds",
1234 sugg,
1235 Applicability::MaybeIncorrect,
1236 );
1237 }
1238 }
1239
1240 fallback |= self.restrict_assoc_type_in_where_clause(span, err);
1241 fallback
1242 }
1243
1244 fn suggest_typo(
1245 &mut self,
1246 err: &mut Diag<'_>,
1247 source: PathSource<'_, 'ast, 'ra>,
1248 path: &[Segment],
1249 following_seg: Option<&Segment>,
1250 span: Span,
1251 base_error: &BaseError,
1252 suggested_candidates: FxHashSet<String>,
1253 ) -> bool {
1254 let is_expected = &|res| source.is_expected(res);
1255 let ident_span = path.last().map_or(span, |ident| ident.ident.span);
1256
1257 if self.suggest_assoc_type_from_bounds(err, source, path, ident_span) {
1261 return false;
1262 }
1263
1264 let typo_sugg =
1265 self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
1266 let mut fallback = false;
1267 let typo_sugg = typo_sugg
1268 .to_opt_suggestion()
1269 .filter(|sugg| !suggested_candidates.contains(sugg.candidate.as_str()));
1270 if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
1271 fallback = true;
1272 match self.diag_metadata.current_let_binding {
1273 Some((pat_sp, Some(ty_sp), None))
1274 if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
1275 {
1276 err.span_suggestion_verbose(
1277 pat_sp.between(ty_sp),
1278 "use `=` if you meant to assign",
1279 " = ",
1280 Applicability::MaybeIncorrect,
1281 );
1282 }
1283 _ => {}
1284 }
1285
1286 let suggestion = self.get_single_associated_item(path, &source, is_expected);
1288 self.r.add_typo_suggestion(err, suggestion, ident_span);
1289 }
1290
1291 if self.let_binding_suggestion(err, ident_span) {
1292 fallback = false;
1293 }
1294
1295 fallback
1296 }
1297
1298 fn suggest_shadowed(
1299 &mut self,
1300 err: &mut Diag<'_>,
1301 source: PathSource<'_, '_, '_>,
1302 path: &[Segment],
1303 following_seg: Option<&Segment>,
1304 span: Span,
1305 ) -> bool {
1306 let is_expected = &|res| source.is_expected(res);
1307 let typo_sugg =
1308 self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
1309 let is_in_same_file = &|sp1, sp2| {
1310 let source_map = self.r.tcx.sess.source_map();
1311 let file1 = source_map.span_to_filename(sp1);
1312 let file2 = source_map.span_to_filename(sp2);
1313 file1 == file2
1314 };
1315 if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
1320 && res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
1321 {
1322 err.span_label(
1323 sugg_span,
1324 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to refer to this {0}",
res.descr()))
})format!("you might have meant to refer to this {}", res.descr()),
1325 );
1326 return true;
1327 }
1328 false
1329 }
1330
1331 fn err_code_special_cases(
1332 &mut self,
1333 err: &mut Diag<'_>,
1334 source: PathSource<'_, '_, '_>,
1335 path: &[Segment],
1336 span: Span,
1337 ) {
1338 if let Some(err_code) = err.code {
1339 if err_code == E0425 {
1340 for label_rib in &self.label_ribs {
1341 for (label_ident, node_id) in &label_rib.bindings {
1342 let ident = path.last().unwrap().ident;
1343 if ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0}", ident))
})format!("'{ident}") == label_ident.to_string() {
1344 err.span_label(label_ident.span, "a label with a similar name exists");
1345 if let PathSource::Expr(Some(Expr {
1346 kind: ExprKind::Break(None, Some(_)),
1347 ..
1348 })) = source
1349 {
1350 err.span_suggestion(
1351 span,
1352 "use the similarly named label",
1353 label_ident.name,
1354 Applicability::MaybeIncorrect,
1355 );
1356 self.diag_metadata.unused_labels.swap_remove(node_id);
1358 }
1359 }
1360 }
1361 }
1362
1363 self.suggest_ident_hidden_by_hygiene(err, path, span);
1364 if let Some(correct) = Self::likely_rust_type(path) {
1366 err.span_suggestion(
1367 span,
1368 "perhaps you intended to use this type",
1369 correct,
1370 Applicability::MaybeIncorrect,
1371 );
1372 }
1373 }
1374 }
1375 }
1376
1377 fn suggest_ident_hidden_by_hygiene(&self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
1378 let [segment] = path else { return };
1379
1380 let ident = segment.ident;
1381 let callsite_span = span.source_callsite();
1382 for rib in self.ribs[ValueNS].iter().rev() {
1383 for (binding_ident, _) in &rib.bindings {
1384 if binding_ident.name == ident.name
1386 && !binding_ident.span.eq_ctxt(span)
1387 && !binding_ident.span.from_expansion()
1388 && binding_ident.span.lo() < callsite_span.lo()
1389 {
1390 err.span_help(
1391 binding_ident.span,
1392 "an identifier with the same name exists, but is not accessible due to macro hygiene",
1393 );
1394 return;
1395 }
1396
1397 if binding_ident.name == ident.name
1399 && binding_ident.span.from_expansion()
1400 && binding_ident.span.source_callsite().eq_ctxt(callsite_span)
1401 && binding_ident.span.source_callsite().lo() < callsite_span.lo()
1402 {
1403 err.span_help(
1404 binding_ident.span,
1405 "an identifier with the same name is defined here, but is not accessible due to macro hygiene",
1406 );
1407 return;
1408 }
1409 }
1410 }
1411 }
1412
1413 fn suggest_self_ty(
1415 &self,
1416 err: &mut Diag<'_>,
1417 source: PathSource<'_, '_, '_>,
1418 path: &[Segment],
1419 span: Span,
1420 ) -> bool {
1421 if !is_self_type(path, source.namespace()) {
1422 return false;
1423 }
1424 err.code(E0411);
1425 err.span_label(span, "`Self` is only available in impls, traits, and type definitions");
1426 if let Some(item) = self.diag_metadata.current_item
1427 && let Some(ident) = item.kind.ident()
1428 {
1429 err.span_label(
1430 ident.span,
1431 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`Self` not allowed in {0} {1}",
item.kind.article(), item.kind.descr()))
})format!("`Self` not allowed in {} {}", item.kind.article(), item.kind.descr()),
1432 );
1433 }
1434 true
1435 }
1436
1437 fn suggest_self_value(
1438 &mut self,
1439 err: &mut Diag<'_>,
1440 source: PathSource<'_, '_, '_>,
1441 path: &[Segment],
1442 span: Span,
1443 ) -> bool {
1444 if !is_self_value(path, source.namespace()) {
1445 return false;
1446 }
1447
1448 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:1448",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(1448u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("smart_resolve_path_fragment: E0424, source={0:?}",
source) as &dyn Value))])
});
} else { ; }
};debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
1449 err.code(E0424);
1450 err.span_label(
1451 span,
1452 match source {
1453 PathSource::Pat => {
1454 "`self` value is a keyword and may not be bound to variables or shadowed"
1455 }
1456 _ => "`self` value is a keyword only available in methods with a `self` parameter",
1457 },
1458 );
1459
1460 if #[allow(non_exhaustive_omitted_patterns)] match source {
PathSource::Pat => true,
_ => false,
}matches!(source, PathSource::Pat) {
1463 return true;
1464 }
1465
1466 let is_assoc_fn = self.self_type_is_available();
1467 let self_from_macro = "a `self` parameter, but a macro invocation can only \
1468 access identifiers it receives from parameters";
1469 if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
1470 if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) {
1475 err.span_label(*fn_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this function has {0}",
self_from_macro))
})format!("this function has {self_from_macro}"));
1476 } else {
1477 let doesnt = if is_assoc_fn {
1478 let (span, sugg) = fn_kind
1479 .decl()
1480 .inputs
1481 .get(0)
1482 .map(|p| (p.span.shrink_to_lo(), "&self, "))
1483 .unwrap_or_else(|| {
1484 let span = fn_kind
1487 .ident()
1488 .map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
1489 (
1490 self.r
1491 .tcx
1492 .sess
1493 .source_map()
1494 .span_through_char(span, '(')
1495 .shrink_to_hi(),
1496 "&self",
1497 )
1498 });
1499 err.span_suggestion_verbose(
1500 span,
1501 "add a `self` receiver parameter to make the associated `fn` a method",
1502 sugg,
1503 Applicability::MaybeIncorrect,
1504 );
1505 "doesn't"
1506 } else {
1507 "can't"
1508 };
1509 if let Some(ident) = fn_kind.ident() {
1510 err.span_label(
1511 ident.span,
1512 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this function {0} have a `self` parameter",
doesnt))
})format!("this function {doesnt} have a `self` parameter"),
1513 );
1514 }
1515 }
1516 } else if let Some(item) = self.diag_metadata.current_item {
1517 if #[allow(non_exhaustive_omitted_patterns)] match item.kind {
ItemKind::Delegation(..) => true,
_ => false,
}matches!(item.kind, ItemKind::Delegation(..)) {
1518 err.span_label(item.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("delegation supports {0}",
self_from_macro))
})format!("delegation supports {self_from_macro}"));
1519 } else {
1520 let span = if let Some(ident) = item.kind.ident() { ident.span } else { item.span };
1521 err.span_label(
1522 span,
1523 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`self` not allowed in {0} {1}",
item.kind.article(), item.kind.descr()))
})format!("`self` not allowed in {} {}", item.kind.article(), item.kind.descr()),
1524 );
1525 }
1526 }
1527 true
1528 }
1529
1530 fn detect_missing_binding_available_from_pattern(
1531 &self,
1532 err: &mut Diag<'_>,
1533 path: &[Segment],
1534 following_seg: Option<&Segment>,
1535 ) {
1536 let [segment] = path else { return };
1537 let None = following_seg else { return };
1538 for rib in self.ribs[ValueNS].iter().rev() {
1539 let patterns_with_skipped_bindings =
1540 self.r.tcx.with_stable_hashing_context(|mut hcx| {
1541 rib.patterns_with_skipped_bindings.to_sorted(&mut hcx, true)
1542 });
1543 for (def_id, spans) in patterns_with_skipped_bindings {
1544 if let DefKind::Struct | DefKind::Variant = self.r.tcx.def_kind(*def_id)
1545 && let Some(fields) = self.r.field_idents(*def_id)
1546 {
1547 for field in fields {
1548 if field.name == segment.ident.name {
1549 if spans.iter().all(|(_, had_error)| had_error.is_err()) {
1550 let multispan: MultiSpan =
1553 spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
1554 err.span_note(
1555 multispan,
1556 "this pattern had a recovered parse error which likely lost \
1557 the expected fields",
1558 );
1559 err.downgrade_to_delayed_bug();
1560 }
1561 let ty = self.r.tcx.item_name(*def_id);
1562 for (span, _) in spans {
1563 err.span_label(
1564 *span,
1565 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this pattern doesn\'t include `{0}`, which is available in `{1}`",
field, ty))
})format!(
1566 "this pattern doesn't include `{field}`, which is \
1567 available in `{ty}`",
1568 ),
1569 );
1570 }
1571 }
1572 }
1573 }
1574 }
1575 }
1576 }
1577
1578 fn suggest_at_operator_in_slice_pat_with_range(&self, err: &mut Diag<'_>, path: &[Segment]) {
1579 let Some(pat) = self.diag_metadata.current_pat else { return };
1580 let (bound, side, range) = match &pat.kind {
1581 ast::PatKind::Range(Some(bound), None, range) => (bound, Side::Start, range),
1582 ast::PatKind::Range(None, Some(bound), range) => (bound, Side::End, range),
1583 _ => return,
1584 };
1585 if let ExprKind::Path(None, range_path) = &bound.kind
1586 && let [segment] = &range_path.segments[..]
1587 && let [s] = path
1588 && segment.ident == s.ident
1589 && segment.ident.span.eq_ctxt(range.span)
1590 {
1591 let (span, snippet) = match side {
1594 Side::Start => (segment.ident.span.between(range.span), " @ ".into()),
1595 Side::End => (range.span.to(segment.ident.span), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} @ ..", segment.ident))
})format!("{} @ ..", segment.ident)),
1596 };
1597 err.subdiagnostic(errors::UnexpectedResUseAtOpInSlicePatWithRangeSugg {
1598 span,
1599 ident: segment.ident,
1600 snippet,
1601 });
1602 }
1603
1604 enum Side {
1605 Start,
1606 End,
1607 }
1608 }
1609
1610 fn suggest_range_struct_destructuring(
1611 &mut self,
1612 err: &mut Diag<'_>,
1613 path: &[Segment],
1614 source: PathSource<'_, '_, '_>,
1615 ) {
1616 if !#[allow(non_exhaustive_omitted_patterns)] match source {
PathSource::Pat | PathSource::TupleStruct(..) | PathSource::Expr(..) =>
true,
_ => false,
}matches!(source, PathSource::Pat | PathSource::TupleStruct(..) | PathSource::Expr(..)) {
1617 return;
1618 }
1619
1620 let Some(pat) = self.diag_metadata.current_pat else { return };
1621 let ast::PatKind::Range(start, end, end_kind) = &pat.kind else { return };
1622
1623 let [segment] = path else { return };
1624 let failing_span = segment.ident.span;
1625
1626 let in_start = start.as_ref().is_some_and(|e| e.span.contains(failing_span));
1627 let in_end = end.as_ref().is_some_and(|e| e.span.contains(failing_span));
1628
1629 if !in_start && !in_end {
1630 return;
1631 }
1632
1633 let start_snippet =
1634 start.as_ref().and_then(|e| self.r.tcx.sess.source_map().span_to_snippet(e.span).ok());
1635 let end_snippet =
1636 end.as_ref().and_then(|e| self.r.tcx.sess.source_map().span_to_snippet(e.span).ok());
1637
1638 let field = |name: &str, val: String| {
1639 if val == name { val } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", name, val))
})format!("{name}: {val}") }
1640 };
1641
1642 let mut resolve_short_name = |short: Symbol, full: &str| -> String {
1643 let ident = Ident::with_dummy_span(short);
1644 let path = Segment::from_path(&Path::from_ident(ident));
1645
1646 match self.resolve_path(&path, Some(TypeNS), None, PathSource::Type) {
1647 PathResult::NonModule(..) => short.to_string(),
1648 _ => full.to_string(),
1649 }
1650 };
1651 let (struct_path, fields) = match (start_snippet, end_snippet, &end_kind.node) {
1653 (Some(start), Some(end), ast::RangeEnd::Excluded) => (
1654 resolve_short_name(sym::Range, "std::ops::Range"),
1655 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[field("start", start), field("end", end)]))vec![field("start", start), field("end", end)],
1656 ),
1657 (Some(start), Some(end), ast::RangeEnd::Included(_)) => (
1658 resolve_short_name(sym::RangeInclusive, "std::ops::RangeInclusive"),
1659 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[field("start", start), field("end", end)]))vec![field("start", start), field("end", end)],
1660 ),
1661 (Some(start), None, _) => (
1662 resolve_short_name(sym::RangeFrom, "std::ops::RangeFrom"),
1663 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[field("start", start)]))vec![field("start", start)],
1664 ),
1665 (None, Some(end), ast::RangeEnd::Excluded) => {
1666 (resolve_short_name(sym::RangeTo, "std::ops::RangeTo"), ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[field("end", end)]))vec![field("end", end)])
1667 }
1668 (None, Some(end), ast::RangeEnd::Included(_)) => (
1669 resolve_short_name(sym::RangeToInclusive, "std::ops::RangeToInclusive"),
1670 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[field("end", end)]))vec![field("end", end)],
1671 ),
1672 _ => return,
1673 };
1674
1675 err.span_suggestion_verbose(
1676 pat.span,
1677 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if you meant to destructure a range use a struct pattern"))
})format!("if you meant to destructure a range use a struct pattern"),
1678 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {{ {1} }}", struct_path,
fields.join(", ")))
})format!("{} {{ {} }}", struct_path, fields.join(", ")),
1679 Applicability::MaybeIncorrect,
1680 );
1681
1682 err.note(
1683 "range patterns match against the start and end of a range; \
1684 to bind the components, use a struct pattern",
1685 );
1686 }
1687
1688 fn suggest_swapping_misplaced_self_ty_and_trait(
1689 &mut self,
1690 err: &mut Diag<'_>,
1691 source: PathSource<'_, 'ast, 'ra>,
1692 res: Option<Res>,
1693 span: Span,
1694 ) {
1695 if let Some((trait_ref, self_ty)) =
1696 self.diag_metadata.currently_processing_impl_trait.clone()
1697 && let TyKind::Path(_, self_ty_path) = &self_ty.kind
1698 && let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1699 self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None, source)
1700 && module.def_kind() == Some(DefKind::Trait)
1701 && trait_ref.path.span == span
1702 && let PathSource::Trait(_) = source
1703 && let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
1704 && let Ok(self_ty_str) = self.r.tcx.sess.source_map().span_to_snippet(self_ty.span)
1705 && let Ok(trait_ref_str) =
1706 self.r.tcx.sess.source_map().span_to_snippet(trait_ref.path.span)
1707 {
1708 err.multipart_suggestion(
1709 "`impl` items mention the trait being implemented first and the type it is being implemented for second",
1710 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)]))vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
1711 Applicability::MaybeIncorrect,
1712 );
1713 }
1714 }
1715
1716 fn explain_functions_in_pattern(
1717 &self,
1718 err: &mut Diag<'_>,
1719 res: Option<Res>,
1720 source: PathSource<'_, '_, '_>,
1721 ) {
1722 let PathSource::TupleStruct(_, _) = source else { return };
1723 let Some(Res::Def(DefKind::Fn, _)) = res else { return };
1724 err.primary_message("expected a pattern, found a function call");
1725 err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch19-00-patterns.html>");
1726 }
1727
1728 fn suggest_changing_type_to_const_param(
1729 &self,
1730 err: &mut Diag<'_>,
1731 res: Option<Res>,
1732 source: PathSource<'_, '_, '_>,
1733 path: &[Segment],
1734 following_seg: Option<&Segment>,
1735 span: Span,
1736 ) {
1737 if let PathSource::Expr(None) = source
1738 && let Some(Res::Def(DefKind::TyParam, _)) = res
1739 && following_seg.is_none()
1740 && let [segment] = path
1741 {
1742 let Some(item) = self.diag_metadata.current_item else { return };
1750 let Some(generics) = item.kind.generics() else { return };
1751 let Some(span) = generics.params.iter().find_map(|param| {
1752 if param.bounds.is_empty() && param.ident.name == segment.ident.name {
1754 Some(param.ident.span)
1755 } else {
1756 None
1757 }
1758 }) else {
1759 return;
1760 };
1761 err.subdiagnostic(errors::UnexpectedResChangeTyParamToConstParamSugg {
1762 before: span.shrink_to_lo(),
1763 after: span.shrink_to_hi(),
1764 });
1765 return;
1766 }
1767 let PathSource::Trait(_) = source else { return };
1768
1769 let applicability = match res {
1771 Some(Res::PrimTy(PrimTy::Int(_) | PrimTy::Uint(_) | PrimTy::Bool | PrimTy::Char)) => {
1772 Applicability::MachineApplicable
1773 }
1774 Some(Res::Def(DefKind::Struct | DefKind::Enum, _))
1778 if self.r.tcx.features().adt_const_params()
1779 || self.r.tcx.features().min_adt_const_params() =>
1780 {
1781 Applicability::MaybeIncorrect
1782 }
1783 _ => return,
1784 };
1785
1786 let Some(item) = self.diag_metadata.current_item else { return };
1787 let Some(generics) = item.kind.generics() else { return };
1788
1789 let param = generics.params.iter().find_map(|param| {
1790 if let [bound] = &*param.bounds
1792 && let ast::GenericBound::Trait(tref) = bound
1793 && tref.modifiers == ast::TraitBoundModifiers::NONE
1794 && tref.span == span
1795 && param.ident.span.eq_ctxt(span)
1796 {
1797 Some(param.ident.span)
1798 } else {
1799 None
1800 }
1801 });
1802
1803 if let Some(param) = param {
1804 err.subdiagnostic(errors::UnexpectedResChangeTyToConstParamSugg {
1805 span: param.shrink_to_lo(),
1806 applicability,
1807 });
1808 }
1809 }
1810
1811 fn suggest_pattern_match_with_let(
1812 &self,
1813 err: &mut Diag<'_>,
1814 source: PathSource<'_, '_, '_>,
1815 span: Span,
1816 ) -> bool {
1817 if let PathSource::Expr(_) = source
1818 && let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }) =
1819 self.diag_metadata.in_if_condition
1820 {
1821 if lhs.is_approximately_pattern() && lhs.span.contains(span) {
1825 err.span_suggestion_verbose(
1826 expr_span.shrink_to_lo(),
1827 "you might have meant to use pattern matching",
1828 "let ",
1829 Applicability::MaybeIncorrect,
1830 );
1831 return true;
1832 }
1833 }
1834 false
1835 }
1836
1837 fn get_single_associated_item(
1838 &mut self,
1839 path: &[Segment],
1840 source: &PathSource<'_, 'ast, 'ra>,
1841 filter_fn: &impl Fn(Res) -> bool,
1842 ) -> Option<TypoSuggestion> {
1843 if let crate::PathSource::TraitItem(_, _) = source {
1844 let mod_path = &path[..path.len() - 1];
1845 if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
1846 self.resolve_path(mod_path, None, None, *source)
1847 {
1848 let targets: Vec<_> = self
1849 .r
1850 .resolutions(module)
1851 .borrow()
1852 .iter()
1853 .filter_map(|(key, resolution)| {
1854 let resolution = resolution.borrow();
1855 resolution.best_decl().map(|binding| binding.res()).and_then(|res| {
1856 if filter_fn(res) {
1857 Some((key.ident.name, resolution.orig_ident_span, res))
1858 } else {
1859 None
1860 }
1861 })
1862 })
1863 .collect();
1864 if let &[(name, orig_ident_span, res)] = targets.as_slice() {
1865 return Some(TypoSuggestion::single_item(name, orig_ident_span, res));
1866 }
1867 }
1868 }
1869 None
1870 }
1871
1872 fn restrict_assoc_type_in_where_clause(&self, span: Span, err: &mut Diag<'_>) -> bool {
1874 let Some(ast::WherePredicate {
1876 kind:
1877 ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
1878 bounded_ty,
1879 bound_generic_params,
1880 bounds,
1881 }),
1882 span: where_span,
1883 ..
1884 }) = self.diag_metadata.current_where_predicate
1885 else {
1886 return false;
1887 };
1888 if !bound_generic_params.is_empty() {
1889 return false;
1890 }
1891
1892 let ast::TyKind::Path(Some(qself), path) = &bounded_ty.kind else { return false };
1894 let Some(partial_res) = self.r.partial_res_map.get(&bounded_ty.id) else { return false };
1896 if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
Some(Res::Def(DefKind::AssocTy, _)) => true,
_ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::AssocTy, _))) {
1897 return false;
1898 }
1899
1900 let peeled_ty = qself.ty.peel_refs();
1901 let ast::TyKind::Path(None, type_param_path) = &peeled_ty.kind else { return false };
1902 let Some(partial_res) = self.r.partial_res_map.get(&peeled_ty.id) else {
1904 return false;
1905 };
1906 if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
Some(Res::Def(DefKind::TyParam, _)) => true,
_ => false,
}matches!(partial_res.full_res(), Some(Res::Def(DefKind::TyParam, _))) {
1907 return false;
1908 }
1909 let ([ast::PathSegment { args: None, .. }], [ast::GenericBound::Trait(poly_trait_ref)]) =
1910 (&type_param_path.segments[..], &bounds[..])
1911 else {
1912 return false;
1913 };
1914 let [ast::PathSegment { ident, args: None, id }] =
1915 &poly_trait_ref.trait_ref.path.segments[..]
1916 else {
1917 return false;
1918 };
1919 if poly_trait_ref.modifiers != ast::TraitBoundModifiers::NONE {
1920 return false;
1921 }
1922 if ident.span == span {
1923 let Some(partial_res) = self.r.partial_res_map.get(&id) else {
1924 return false;
1925 };
1926 if !#[allow(non_exhaustive_omitted_patterns)] match partial_res.full_res() {
Some(Res::Def(..)) => true,
_ => false,
}matches!(partial_res.full_res(), Some(Res::Def(..))) {
1927 return false;
1928 }
1929
1930 let Some(new_where_bound_predicate) =
1931 mk_where_bound_predicate(path, poly_trait_ref, &qself.ty)
1932 else {
1933 return false;
1934 };
1935 err.span_suggestion_verbose(
1936 *where_span,
1937 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("constrain the associated type to `{0}`",
ident))
})format!("constrain the associated type to `{ident}`"),
1938 where_bound_predicate_to_string(&new_where_bound_predicate),
1939 Applicability::MaybeIncorrect,
1940 );
1941 }
1942 true
1943 }
1944
1945 fn call_has_self_arg(&self, source: PathSource<'_, '_, '_>) -> Option<(Span, Option<Span>)> {
1948 let mut has_self_arg = None;
1949 if let PathSource::Expr(Some(parent)) = source
1950 && let ExprKind::Call(_, args) = &parent.kind
1951 && !args.is_empty()
1952 {
1953 let mut expr_kind = &args[0].kind;
1954 loop {
1955 match expr_kind {
1956 ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
1957 if arg_name.segments[0].ident.name == kw::SelfLower {
1958 let call_span = parent.span;
1959 let tail_args_span = if args.len() > 1 {
1960 Some(Span::new(
1961 args[1].span.lo(),
1962 args.last().unwrap().span.hi(),
1963 call_span.ctxt(),
1964 None,
1965 ))
1966 } else {
1967 None
1968 };
1969 has_self_arg = Some((call_span, tail_args_span));
1970 }
1971 break;
1972 }
1973 ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
1974 _ => break,
1975 }
1976 }
1977 }
1978 has_self_arg
1979 }
1980
1981 fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
1982 let sm = self.r.tcx.sess.source_map();
1987 if let Some(open_brace_span) = sm.span_followed_by(span, "{") {
1988 let close_brace_span =
1991 sm.span_to_next_source(open_brace_span).ok().and_then(|next_source| {
1992 let mut depth: u32 = 1;
1994 let offset = next_source.char_indices().find_map(|(i, c)| {
1995 match c {
1996 '{' => depth += 1,
1997 '}' if depth == 1 => return Some(i),
1998 '}' => depth -= 1,
1999 _ => {}
2000 }
2001 None
2002 })?;
2003 let start = open_brace_span.hi() + rustc_span::BytePos(offset as u32);
2004 Some(open_brace_span.with_lo(start).with_hi(start + rustc_span::BytePos(1)))
2005 });
2006 let closing_brace = close_brace_span.map(|sp| span.to(sp));
2007 (true, closing_brace)
2008 } else {
2009 (false, None)
2010 }
2011 }
2012
2013 fn update_err_for_private_tuple_struct_fields(
2014 &mut self,
2015 err: &mut Diag<'_>,
2016 source: &PathSource<'_, '_, '_>,
2017 def_id: DefId,
2018 ) -> Option<Vec<Span>> {
2019 match source {
2020 PathSource::TupleStruct(_, pattern_spans) => {
2022 err.primary_message(
2023 "cannot match against a tuple struct which contains private fields",
2024 );
2025
2026 Some(Vec::from(*pattern_spans))
2028 }
2029 PathSource::Expr(Some(Expr {
2031 kind: ExprKind::Call(path, args),
2032 span: call_span,
2033 ..
2034 })) => {
2035 err.primary_message(
2036 "cannot initialize a tuple struct which contains private fields",
2037 );
2038 self.suggest_alternative_construction_methods(
2039 def_id,
2040 err,
2041 path.span,
2042 *call_span,
2043 &args[..],
2044 );
2045
2046 self.r
2047 .field_idents(def_id)
2048 .map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
2049 }
2050 _ => None,
2051 }
2052 }
2053
2054 fn smart_resolve_context_dependent_help(
2058 &mut self,
2059 err: &mut Diag<'_>,
2060 span: Span,
2061 source: PathSource<'_, '_, '_>,
2062 path: &[Segment],
2063 res: Res,
2064 path_str: &str,
2065 fallback_label: &str,
2066 ) -> bool {
2067 let ns = source.namespace();
2068 let is_expected = &|res| source.is_expected(res);
2069
2070 let path_sep = |this: &Self, err: &mut Diag<'_>, expr: &Expr, kind: DefKind| {
2071 const MESSAGE: &str = "use the path separator to refer to an item";
2072
2073 let (lhs_span, rhs_span) = match &expr.kind {
2074 ExprKind::Field(base, ident) => (base.span, ident.span),
2075 ExprKind::MethodCall(MethodCall { receiver, span, .. }) => (receiver.span, *span),
2076 _ => return false,
2077 };
2078
2079 if lhs_span.eq_ctxt(rhs_span) {
2080 err.span_suggestion_verbose(
2081 lhs_span.between(rhs_span),
2082 MESSAGE,
2083 "::",
2084 Applicability::MaybeIncorrect,
2085 );
2086 true
2087 } else if #[allow(non_exhaustive_omitted_patterns)] match kind {
DefKind::Struct | DefKind::TyAlias => true,
_ => false,
}matches!(kind, DefKind::Struct | DefKind::TyAlias)
2088 && let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
2089 && let Ok(snippet) = this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
2090 {
2091 err.span_suggestion_verbose(
2095 lhs_source_span.until(rhs_span),
2096 MESSAGE,
2097 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>::", snippet))
})format!("<{snippet}>::"),
2098 Applicability::MaybeIncorrect,
2099 );
2100 true
2101 } else {
2102 false
2108 }
2109 };
2110
2111 let find_span = |source: &PathSource<'_, '_, '_>, err: &mut Diag<'_>| {
2112 match source {
2113 PathSource::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }))
2114 | PathSource::TupleStruct(span, _) => {
2115 err.span(*span);
2118 *span
2119 }
2120 _ => span,
2121 }
2122 };
2123
2124 let bad_struct_syntax_suggestion = |this: &mut Self, err: &mut Diag<'_>, def_id: DefId| {
2125 let (followed_by_brace, closing_brace) = this.followed_by_brace(span);
2126
2127 match source {
2128 PathSource::Expr(Some(
2129 parent @ Expr { kind: ExprKind::Field(..) | ExprKind::MethodCall(..), .. },
2130 )) if path_sep(this, err, parent, DefKind::Struct) => {}
2131 PathSource::Expr(
2132 None
2133 | Some(Expr {
2134 kind:
2135 ExprKind::Path(..)
2136 | ExprKind::Binary(..)
2137 | ExprKind::Unary(..)
2138 | ExprKind::If(..)
2139 | ExprKind::While(..)
2140 | ExprKind::ForLoop { .. }
2141 | ExprKind::Match(..),
2142 ..
2143 }),
2144 ) if followed_by_brace => {
2145 if let Some(sp) = closing_brace {
2146 err.span_label(span, fallback_label.to_string());
2147 err.multipart_suggestion(
2148 "surround the struct literal with parentheses",
2149 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(sp.shrink_to_lo(), "(".to_string()),
(sp.shrink_to_hi(), ")".to_string())]))vec![
2150 (sp.shrink_to_lo(), "(".to_string()),
2151 (sp.shrink_to_hi(), ")".to_string()),
2152 ],
2153 Applicability::MaybeIncorrect,
2154 );
2155 } else {
2156 err.span_label(
2157 span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might want to surround a struct literal with parentheses: `({0} {{ /* fields */ }})`?",
path_str))
})format!(
2159 "you might want to surround a struct literal with parentheses: \
2160 `({path_str} {{ /* fields */ }})`?"
2161 ),
2162 );
2163 }
2164 }
2165 PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
2166 let span = find_span(&source, err);
2167 err.span_label(this.r.def_span(def_id), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
})format!("`{path_str}` defined here"));
2168
2169 let (tail, descr, applicability, old_fields) = match source {
2170 PathSource::Pat => ("", "pattern", Applicability::MachineApplicable, None),
2171 PathSource::TupleStruct(_, args) => (
2172 "",
2173 "pattern",
2174 Applicability::MachineApplicable,
2175 Some(
2176 args.iter()
2177 .map(|a| this.r.tcx.sess.source_map().span_to_snippet(*a).ok())
2178 .collect::<Vec<Option<String>>>(),
2179 ),
2180 ),
2181 _ => (": val", "literal", Applicability::HasPlaceholders, None),
2182 };
2183
2184 let has_private_fields = match def_id.as_local() {
2186 Some(def_id) => this.r.struct_ctors.get(&def_id).is_some_and(|ctor| {
2187 ctor.has_private_fields(this.parent_scope.module, this.r)
2188 }),
2189 None => this.r.tcx.associated_item_def_ids(def_id).iter().any(|field_id| {
2190 let vis = this.r.tcx.visibility(*field_id);
2191 !this.r.is_accessible_from(vis, this.parent_scope.module)
2192 }),
2193 };
2194 if !has_private_fields {
2195 let fields = this.r.field_idents(def_id);
2198 let has_fields = fields.as_ref().is_some_and(|f| !f.is_empty());
2199
2200 if let PathSource::Expr(Some(Expr {
2201 kind: ExprKind::Call(path, args),
2202 span,
2203 ..
2204 })) = source
2205 && !args.is_empty()
2206 && let Some(fields) = &fields
2207 && args.len() == fields.len()
2208 {
2210 let path_span = path.span;
2211 let mut parts = Vec::new();
2212
2213 parts.push((
2215 path_span.shrink_to_hi().until(args[0].span),
2216 "{".to_owned(),
2217 ));
2218
2219 for (field, arg) in fields.iter().zip(args.iter()) {
2220 parts.push((arg.span.shrink_to_lo(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: ", field))
})format!("{}: ", field)));
2222 }
2223
2224 parts.push((
2226 args.last().unwrap().span.shrink_to_hi().until(span.shrink_to_hi()),
2227 "}".to_owned(),
2228 ));
2229
2230 err.multipart_suggestion(
2231 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use struct {0} syntax instead of calling",
descr))
})format!("use struct {descr} syntax instead of calling"),
2232 parts,
2233 applicability,
2234 );
2235 } else {
2236 let (fields, applicability) = match fields {
2237 Some(fields) => {
2238 let fields = if let Some(old_fields) = old_fields {
2239 fields
2240 .iter()
2241 .enumerate()
2242 .map(|(idx, new)| (new, old_fields.get(idx)))
2243 .map(|(new, old)| {
2244 if let Some(Some(old)) = old
2245 && new.as_str() != old
2246 {
2247 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: {1}", new, old))
})format!("{new}: {old}")
2248 } else {
2249 new.to_string()
2250 }
2251 })
2252 .collect::<Vec<String>>()
2253 } else {
2254 fields
2255 .iter()
2256 .map(|f| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", f, tail))
})format!("{f}{tail}"))
2257 .collect::<Vec<String>>()
2258 };
2259
2260 (fields.join(", "), applicability)
2261 }
2262 None => {
2263 ("/* fields */".to_string(), Applicability::HasPlaceholders)
2264 }
2265 };
2266 let pad = if has_fields { " " } else { "" };
2267 err.span_suggestion(
2268 span,
2269 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use struct {0} syntax instead",
descr))
})format!("use struct {descr} syntax instead"),
2270 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {{{1}{2}{1}}}", path_str, pad,
fields))
})format!("{path_str} {{{pad}{fields}{pad}}}"),
2271 applicability,
2272 );
2273 }
2274 }
2275 if let PathSource::Expr(Some(Expr {
2276 kind: ExprKind::Call(path, args),
2277 span: call_span,
2278 ..
2279 })) = source
2280 {
2281 this.suggest_alternative_construction_methods(
2282 def_id,
2283 err,
2284 path.span,
2285 *call_span,
2286 &args[..],
2287 );
2288 }
2289 }
2290 _ => {
2291 err.span_label(span, fallback_label.to_string());
2292 }
2293 }
2294 };
2295
2296 match (res, source) {
2297 (
2298 Res::Def(DefKind::Macro(kinds), def_id),
2299 PathSource::Expr(Some(Expr {
2300 kind: ExprKind::Index(..) | ExprKind::Call(..), ..
2301 }))
2302 | PathSource::Struct(_),
2303 ) if kinds.contains(MacroKinds::BANG) => {
2304 let suggestable = def_id.is_local()
2306 || self.r.tcx.lookup_stability(def_id).is_none_or(|s| s.is_stable());
2307
2308 err.span_label(span, fallback_label.to_string());
2309
2310 if path
2312 .last()
2313 .is_some_and(|segment| !segment.has_generic_args && !segment.has_lifetime_args)
2314 && suggestable
2315 {
2316 err.span_suggestion_verbose(
2317 span.shrink_to_hi(),
2318 "use `!` to invoke the macro",
2319 "!",
2320 Applicability::MaybeIncorrect,
2321 );
2322 }
2323
2324 if path_str == "try" && span.is_rust_2015() {
2325 err.note("if you want the `try` keyword, you need Rust 2018 or later");
2326 }
2327 }
2328 (Res::Def(DefKind::Macro(kinds), _), _) if kinds.contains(MacroKinds::BANG) => {
2329 err.span_label(span, fallback_label.to_string());
2330 }
2331 (Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
2332 err.span_label(span, "type aliases cannot be used as traits");
2333 if self.r.tcx.sess.is_nightly_build() {
2334 let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
2335 `type` alias";
2336 let span = self.r.def_span(def_id);
2337 if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) {
2338 let snip = snip.replacen("type", "trait", 1);
2341 err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
2342 } else {
2343 err.span_help(span, msg);
2344 }
2345 }
2346 }
2347 (
2348 Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _),
2349 PathSource::Expr(Some(parent)),
2350 ) if path_sep(self, err, parent, kind) => {
2351 return true;
2352 }
2353 (
2354 Res::Def(DefKind::Enum, def_id),
2355 PathSource::TupleStruct(..) | PathSource::Expr(..),
2356 ) => {
2357 self.suggest_using_enum_variant(err, source, def_id, span);
2358 }
2359 (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
2360 if let PathSource::Expr(Some(parent)) = source
2361 && let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind
2362 {
2363 bad_struct_syntax_suggestion(self, err, def_id);
2364 return true;
2365 }
2366 let Some(ctor) = self.r.struct_ctor(def_id) else {
2367 bad_struct_syntax_suggestion(self, err, def_id);
2368 return true;
2369 };
2370
2371 let is_accessible = self.r.is_accessible_from(ctor.vis, self.parent_scope.module);
2374 if is_accessible
2375 && let mod_path = &path[..path.len() - 1]
2376 && let PathResult::Module(ModuleOrUniformRoot::Module(import_mod)) =
2377 self.resolve_path(mod_path, Some(TypeNS), None, PathSource::Module)
2378 && ctor.has_private_fields(import_mod, self.r)
2379 && let Ok(import_decl) = self.r.cm().maybe_resolve_ident_in_module(
2380 ModuleOrUniformRoot::Module(import_mod),
2381 path.last().unwrap().ident,
2382 TypeNS,
2383 &self.parent_scope,
2384 None,
2385 )
2386 {
2387 err.span_note(
2388 import_decl.span,
2389 "the type is accessed through this re-export, but the type's constructor \
2390 is not visible in this import's scope due to private fields",
2391 );
2392 if !ctor.has_private_fields(self.parent_scope.module, self.r) {
2393 err.span_suggestion_verbose(
2394 span,
2395 "the type can be constructed directly, because its fields are \
2396 available from the current scope",
2397 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("crate{0}",
self.r.tcx.def_path(def_id).to_string_no_crate_verbose()))
})format!(
2401 "crate{}", self.r.tcx.def_path(def_id).to_string_no_crate_verbose(),
2403 ),
2404 Applicability::MachineApplicable,
2405 );
2406 }
2407 self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
2408 }
2409 if !is_expected(ctor.res) || is_accessible {
2410 return true;
2411 }
2412
2413 let field_spans =
2414 self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
2415
2416 if let Some(spans) = field_spans
2417 .filter(|spans| spans.len() > 0 && ctor.field_visibilities.len() == spans.len())
2418 {
2419 let non_visible_spans: Vec<Span> = iter::zip(&ctor.field_visibilities, &spans)
2420 .filter(|(vis, _)| {
2421 !self.r.is_accessible_from(**vis, self.parent_scope.module)
2422 })
2423 .map(|(_, span)| *span)
2424 .collect();
2425
2426 if non_visible_spans.len() > 0 {
2427 if let Some(fields) = self.r.field_visibility_spans.get(&def_id) {
2428 err.multipart_suggestion(
2429 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider making the field{0} publicly accessible",
if fields.len() == 1 { "" } else { "s" }))
})format!(
2430 "consider making the field{} publicly accessible",
2431 pluralize!(fields.len())
2432 ),
2433 fields.iter().map(|span| (*span, "pub ".to_string())).collect(),
2434 Applicability::MaybeIncorrect,
2435 );
2436 }
2437
2438 let mut m: MultiSpan = non_visible_spans.clone().into();
2439 non_visible_spans
2440 .into_iter()
2441 .for_each(|s| m.push_span_label(s, "private field"));
2442 err.span_note(m, "constructor is not visible here due to private fields");
2443 }
2444
2445 return true;
2446 }
2447
2448 err.span_label(span, "constructor is not visible here due to private fields");
2449 }
2450 (Res::Def(DefKind::Union | DefKind::Variant, def_id), _) if ns == ValueNS => {
2451 bad_struct_syntax_suggestion(self, err, def_id);
2452 }
2453 (Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
2454 match source {
2455 PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
2456 let span = find_span(&source, err);
2457 err.span_label(
2458 self.r.def_span(def_id),
2459 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
})format!("`{path_str}` defined here"),
2460 );
2461 err.span_suggestion(
2462 span,
2463 "use this syntax instead",
2464 path_str,
2465 Applicability::MaybeIncorrect,
2466 );
2467 }
2468 _ => return false,
2469 }
2470 }
2471 (Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => {
2472 let def_id = self.r.tcx.parent(ctor_def_id);
2473 err.span_label(self.r.def_span(def_id), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` defined here", path_str))
})format!("`{path_str}` defined here"));
2474 let fields = self.r.field_idents(def_id).map_or_else(
2475 || "/* fields */".to_string(),
2476 |field_ids| ::alloc::vec::from_elem("_", field_ids.len())vec!["_"; field_ids.len()].join(", "),
2477 );
2478 err.span_suggestion(
2479 span,
2480 "use the tuple variant pattern syntax instead",
2481 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}({1})", path_str, fields))
})format!("{path_str}({fields})"),
2482 Applicability::HasPlaceholders,
2483 );
2484 }
2485 (Res::SelfTyParam { .. } | Res::SelfTyAlias { .. }, _) if ns == ValueNS => {
2486 err.span_label(span, fallback_label.to_string());
2487 err.note("can't use `Self` as a constructor, you must use the implemented struct");
2488 }
2489 (
2490 Res::Def(DefKind::TyAlias | DefKind::AssocTy, _),
2491 PathSource::TraitItem(ValueNS, PathSource::TupleStruct(whole, args)),
2492 ) => {
2493 err.note("can't use a type alias as tuple pattern");
2494
2495 let mut suggestion = Vec::new();
2496
2497 if let &&[first, ..] = args
2498 && let &&[.., last] = args
2499 {
2500 suggestion.extend([
2501 (span.between(first), " { 0: ".to_owned()),
2507 (last.between(whole.shrink_to_hi()), " }".to_owned()),
2508 ]);
2509
2510 suggestion.extend(
2511 args.iter()
2512 .enumerate()
2513 .skip(1) .map(|(index, &arg)| (arg.shrink_to_lo(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: ", index))
})format!("{index}: "))),
2515 )
2516 } else {
2517 suggestion.push((span.between(whole.shrink_to_hi()), " {}".to_owned()));
2518 }
2519
2520 err.multipart_suggestion(
2521 "use struct pattern instead",
2522 suggestion,
2523 Applicability::MachineApplicable,
2524 );
2525 }
2526 (
2527 Res::Def(DefKind::TyAlias | DefKind::AssocTy, _),
2528 PathSource::TraitItem(
2529 ValueNS,
2530 PathSource::Expr(Some(ast::Expr {
2531 span: whole,
2532 kind: ast::ExprKind::Call(_, args),
2533 ..
2534 })),
2535 ),
2536 ) => {
2537 err.note("can't use a type alias as a constructor");
2538
2539 let mut suggestion = Vec::new();
2540
2541 if let [first, ..] = &**args
2542 && let [.., last] = &**args
2543 {
2544 suggestion.extend([
2545 (span.between(first.span), " { 0: ".to_owned()),
2551 (last.span.between(whole.shrink_to_hi()), " }".to_owned()),
2552 ]);
2553
2554 suggestion.extend(
2555 args.iter()
2556 .enumerate()
2557 .skip(1) .map(|(index, arg)| (arg.span.shrink_to_lo(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: ", index))
})format!("{index}: "))),
2559 )
2560 } else {
2561 suggestion.push((span.between(whole.shrink_to_hi()), " {}".to_owned()));
2562 }
2563
2564 err.multipart_suggestion(
2565 "use struct expression instead",
2566 suggestion,
2567 Applicability::MachineApplicable,
2568 );
2569 }
2570 _ => return false,
2571 }
2572 true
2573 }
2574
2575 fn suggest_alternative_construction_methods(
2576 &mut self,
2577 def_id: DefId,
2578 err: &mut Diag<'_>,
2579 path_span: Span,
2580 call_span: Span,
2581 args: &[Box<Expr>],
2582 ) {
2583 if def_id.is_local() {
2584 return;
2586 }
2587 let mut items = self
2590 .r
2591 .tcx
2592 .inherent_impls(def_id)
2593 .iter()
2594 .flat_map(|&i| self.r.tcx.associated_items(i).in_definition_order())
2595 .filter(|item| item.is_fn() && !item.is_method())
2597 .filter_map(|item| {
2598 let fn_sig = self.r.tcx.fn_sig(item.def_id).skip_binder();
2600 let ret_ty = fn_sig.output().skip_binder();
2602 let ty::Adt(def, _args) = ret_ty.kind() else {
2603 return None;
2604 };
2605 let input_len = fn_sig.inputs().skip_binder().len();
2606 if def.did() != def_id {
2607 return None;
2608 }
2609 let name = item.name();
2610 let order = !name.as_str().starts_with("new");
2611 Some((order, name, input_len))
2612 })
2613 .collect::<Vec<_>>();
2614 items.sort_by_key(|(order, _, _)| *order);
2615 let suggestion = |name, args| {
2616 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("::{1}({0})",
std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "),
name))
})format!("::{name}({})", std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "))
2617 };
2618 match &items[..] {
2619 [] => {}
2620 [(_, name, len)] if *len == args.len() => {
2621 err.span_suggestion_verbose(
2622 path_span.shrink_to_hi(),
2623 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use the `{0}` associated function",
name))
})format!("you might have meant to use the `{name}` associated function",),
2624 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("::{0}", name))
})format!("::{name}"),
2625 Applicability::MaybeIncorrect,
2626 );
2627 }
2628 [(_, name, len)] => {
2629 err.span_suggestion_verbose(
2630 path_span.shrink_to_hi().with_hi(call_span.hi()),
2631 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use the `{0}` associated function",
name))
})format!("you might have meant to use the `{name}` associated function",),
2632 suggestion(name, *len),
2633 Applicability::MaybeIncorrect,
2634 );
2635 }
2636 _ => {
2637 err.span_suggestions_with_style(
2638 path_span.shrink_to_hi().with_hi(call_span.hi()),
2639 "you might have meant to use an associated function to build this type",
2640 items.iter().map(|(_, name, len)| suggestion(name, *len)),
2641 Applicability::MaybeIncorrect,
2642 SuggestionStyle::ShowAlways,
2643 );
2644 }
2645 }
2646 let default_trait = self
2654 .r
2655 .lookup_import_candidates(
2656 Ident::with_dummy_span(sym::Default),
2657 Namespace::TypeNS,
2658 &self.parent_scope,
2659 &|res: Res| #[allow(non_exhaustive_omitted_patterns)] match res {
Res::Def(DefKind::Trait, _) => true,
_ => false,
}matches!(res, Res::Def(DefKind::Trait, _)),
2660 )
2661 .iter()
2662 .filter_map(|candidate| candidate.did)
2663 .find(|did| {
{
'done:
{
for i in
::rustc_hir::attrs::HasAttrs::get_attrs(*did, &self.r.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDiagnosticItem(sym::Default))
=> {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(self.r.tcx, *did, RustcDiagnosticItem(sym::Default)));
2664 let Some(default_trait) = default_trait else {
2665 return;
2666 };
2667 if self
2668 .r
2669 .extern_crate_map
2670 .items()
2671 .flat_map(|(_, crate_)| {
2673 UnordItems::new(
2674 self.r.tcx.implementations_of_trait((*crate_, default_trait)).into_iter(),
2675 )
2676 })
2677 .filter_map(|(_, simplified_self_ty)| *simplified_self_ty)
2678 .filter_map(|simplified_self_ty| match simplified_self_ty {
2679 SimplifiedType::Adt(did) => Some(did),
2680 _ => None,
2681 })
2682 .any(|did| did == def_id)
2683 {
2684 err.multipart_suggestion(
2685 "consider using the `Default` trait",
2686 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(path_span.shrink_to_lo(), "<".to_string()),
(path_span.shrink_to_hi().with_hi(call_span.hi()),
" as std::default::Default>::default()".to_string())]))vec![
2687 (path_span.shrink_to_lo(), "<".to_string()),
2688 (
2689 path_span.shrink_to_hi().with_hi(call_span.hi()),
2690 " as std::default::Default>::default()".to_string(),
2691 ),
2692 ],
2693 Applicability::MaybeIncorrect,
2694 );
2695 }
2696 }
2697
2698 pub(crate) fn find_similarly_named_assoc_item(
2701 &mut self,
2702 ident: Symbol,
2703 kind: &AssocItemKind,
2704 ) -> Option<Symbol> {
2705 let (module, _) = self.current_trait_ref.as_ref()?;
2706 if ident == kw::Underscore {
2707 return None;
2709 }
2710
2711 let targets = self
2712 .r
2713 .resolutions(*module)
2714 .borrow()
2715 .iter()
2716 .filter_map(|(key, res)| res.borrow().best_decl().map(|binding| (key, binding.res())))
2717 .filter(|(_, res)| match (kind, res) {
2718 (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst { .. }, _)) => true,
2719 (AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,
2720 (AssocItemKind::Type(..), Res::Def(DefKind::AssocTy, _)) => true,
2721 (AssocItemKind::Delegation(_), Res::Def(DefKind::AssocFn, _)) => true,
2722 _ => false,
2723 })
2724 .map(|(key, _)| key.ident.name)
2725 .collect::<Vec<_>>();
2726
2727 find_best_match_for_name(&targets, ident, None)
2728 }
2729
2730 fn lookup_assoc_candidate<FilterFn>(
2731 &mut self,
2732 ident: Ident,
2733 ns: Namespace,
2734 filter_fn: FilterFn,
2735 called: bool,
2736 ) -> Option<AssocSuggestion>
2737 where
2738 FilterFn: Fn(Res) -> bool,
2739 {
2740 fn extract_node_id(t: &Ty) -> Option<NodeId> {
2741 match t.kind {
2742 TyKind::Path(None, _) => Some(t.id),
2743 TyKind::Ref(_, ref mut_ty) => extract_node_id(&mut_ty.ty),
2744 _ => None,
2748 }
2749 }
2750 if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
2752 if let Some(node_id) =
2753 self.diag_metadata.current_self_type.as_ref().and_then(extract_node_id)
2754 && let Some(resolution) = self.r.partial_res_map.get(&node_id)
2755 && let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) = resolution.full_res()
2756 && let Some(fields) = self.r.field_idents(did)
2757 && let Some(field) = fields.iter().find(|id| ident.name == id.name)
2758 {
2759 return Some(AssocSuggestion::Field(field.span));
2761 }
2762 }
2763
2764 if let Some(items) = self.diag_metadata.current_trait_assoc_items {
2765 for assoc_item in items {
2766 if let Some(assoc_ident) = assoc_item.kind.ident()
2767 && assoc_ident == ident
2768 {
2769 return Some(match &assoc_item.kind {
2770 ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
2771 ast::AssocItemKind::Fn(ast::Fn { sig, .. }) if sig.decl.has_self() => {
2772 AssocSuggestion::MethodWithSelf { called }
2773 }
2774 ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
2775 ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
2776 ast::AssocItemKind::Delegation(..)
2777 if self
2778 .r
2779 .owners
2780 .get(&assoc_item.id)
2781 .and_then(|o| self.r.delegation_fn_sigs.get(&o.def_id))
2782 .is_some_and(|sig| sig.has_self) =>
2783 {
2784 AssocSuggestion::MethodWithSelf { called }
2785 }
2786 ast::AssocItemKind::Delegation(..) => AssocSuggestion::AssocFn { called },
2787 ast::AssocItemKind::MacCall(_) | ast::AssocItemKind::DelegationMac(..) => {
2788 continue;
2789 }
2790 });
2791 }
2792 }
2793 }
2794
2795 if let Some((module, _)) = self.current_trait_ref
2797 && let Ok(binding) = self.r.cm().maybe_resolve_ident_in_module(
2798 ModuleOrUniformRoot::Module(module),
2799 ident,
2800 ns,
2801 &self.parent_scope,
2802 None,
2803 )
2804 {
2805 let res = binding.res();
2806 if filter_fn(res) {
2807 match res {
2808 Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => {
2809 let has_self = match def_id.as_local() {
2810 Some(def_id) => self
2811 .r
2812 .delegation_fn_sigs
2813 .get(&def_id)
2814 .is_some_and(|sig| sig.has_self),
2815 None => {
2816 self.r.tcx.fn_arg_idents(def_id).first().is_some_and(|&ident| {
2817 #[allow(non_exhaustive_omitted_patterns)] match ident {
Some(Ident { name: kw::SelfLower, .. }) => true,
_ => false,
}matches!(ident, Some(Ident { name: kw::SelfLower, .. }))
2818 })
2819 }
2820 };
2821 if has_self {
2822 return Some(AssocSuggestion::MethodWithSelf { called });
2823 } else {
2824 return Some(AssocSuggestion::AssocFn { called });
2825 }
2826 }
2827 Res::Def(DefKind::AssocConst { .. }, _) => {
2828 return Some(AssocSuggestion::AssocConst);
2829 }
2830 Res::Def(DefKind::AssocTy, _) => {
2831 return Some(AssocSuggestion::AssocType);
2832 }
2833 _ => {}
2834 }
2835 }
2836 }
2837
2838 None
2839 }
2840
2841 fn lookup_typo_candidate(
2842 &mut self,
2843 path: &[Segment],
2844 following_seg: Option<&Segment>,
2845 ns: Namespace,
2846 filter_fn: &impl Fn(Res) -> bool,
2847 ) -> TypoCandidate {
2848 let mut names = Vec::new();
2849 if let [segment] = path {
2850 let mut ctxt = segment.ident.span.ctxt();
2851
2852 for rib in self.ribs[ns].iter().rev() {
2855 let rib_ctxt = if rib.kind.contains_params() {
2856 ctxt.normalize_to_macros_2_0()
2857 } else {
2858 ctxt.normalize_to_macro_rules()
2859 };
2860
2861 for (ident, &res) in &rib.bindings {
2863 if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
2864 names.push(TypoSuggestion::new(ident.name, ident.span, res));
2865 }
2866 }
2867
2868 if let RibKind::Block(Some(module)) = rib.kind {
2869 self.r.add_module_candidates(
2870 module.to_module(),
2871 &mut names,
2872 &filter_fn,
2873 Some(ctxt),
2874 );
2875 } else if let RibKind::Module(module) = rib.kind {
2876 let parent_scope =
2878 &ParentScope { module: module.to_module(), ..self.parent_scope };
2879 self.r.add_scope_set_candidates(
2880 &mut names,
2881 ScopeSet::All(ns),
2882 parent_scope,
2883 segment.ident.span.with_ctxt(ctxt),
2884 filter_fn,
2885 );
2886 break;
2887 }
2888
2889 if let RibKind::MacroDefinition(def) = rib.kind
2890 && def == self.r.macro_def(ctxt)
2891 {
2892 ctxt.remove_mark();
2895 }
2896 }
2897 } else {
2898 let mod_path = &path[..path.len() - 1];
2900 if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
2901 self.resolve_path(mod_path, Some(TypeNS), None, PathSource::Type)
2902 {
2903 self.r.add_module_candidates(module, &mut names, &filter_fn, None);
2904 }
2905 }
2906
2907 if let Some(following_seg) = following_seg {
2909 names.retain(|suggestion| match suggestion.res {
2910 Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _) => {
2911 suggestion.candidate != following_seg.ident.name
2913 }
2914 Res::Def(DefKind::Mod, def_id) => {
2915 let module = self.r.expect_module(def_id);
2916 self.r
2917 .resolutions(module)
2918 .borrow()
2919 .iter()
2920 .any(|(key, _)| key.ident.name == following_seg.ident.name)
2921 }
2922 _ => true,
2923 });
2924 }
2925 let name = path[path.len() - 1].ident.name;
2926 names.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));
2928
2929 match find_best_match_for_name(
2930 &names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
2931 name,
2932 None,
2933 ) {
2934 Some(found) => {
2935 let Some(sugg) = names.into_iter().find(|suggestion| suggestion.candidate == found)
2936 else {
2937 return TypoCandidate::None;
2938 };
2939 if found == name {
2940 TypoCandidate::Shadowed(sugg.res, sugg.span)
2941 } else {
2942 TypoCandidate::Typo(sugg)
2943 }
2944 }
2945 _ => TypoCandidate::None,
2946 }
2947 }
2948
2949 fn likely_rust_type(path: &[Segment]) -> Option<Symbol> {
2952 let name = path[path.len() - 1].ident.as_str();
2953 Some(match name {
2955 "byte" => sym::u8, "short" => sym::i16,
2957 "Bool" => sym::bool,
2958 "Boolean" => sym::bool,
2959 "boolean" => sym::bool,
2960 "int" => sym::i32,
2961 "long" => sym::i64,
2962 "float" => sym::f32,
2963 "double" => sym::f64,
2964 _ => return None,
2965 })
2966 }
2967
2968 fn let_binding_suggestion(&self, err: &mut Diag<'_>, ident_span: Span) -> bool {
2971 if ident_span.from_expansion() {
2972 return false;
2973 }
2974
2975 if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) = self.diag_metadata.in_assignment
2977 && let ast::ExprKind::Path(None, ref path) = lhs.kind
2978 && self.r.tcx.sess.source_map().is_line_before_span_empty(ident_span)
2979 {
2980 let (span, text) = match path.segments.first() {
2981 Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
2982 let name = name.trim_prefix('_');
2984 (ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("let {0}", name))
})format!("let {name}"))
2985 }
2986 _ => (ident_span.shrink_to_lo(), "let ".to_string()),
2987 };
2988
2989 err.span_suggestion_verbose(
2990 span,
2991 "you might have meant to introduce a new binding",
2992 text,
2993 Applicability::MaybeIncorrect,
2994 );
2995 return true;
2996 }
2997
2998 if err.code == Some(E0423)
3001 && let Some((let_span, None, Some(val_span))) = self.diag_metadata.current_let_binding
3002 && val_span.contains(ident_span)
3003 && val_span.lo() == ident_span.lo()
3004 {
3005 err.span_suggestion_verbose(
3006 let_span.shrink_to_hi().to(val_span.shrink_to_lo()),
3007 "you might have meant to use `:` for type annotation",
3008 ": ",
3009 Applicability::MaybeIncorrect,
3010 );
3011 return true;
3012 }
3013 false
3014 }
3015
3016 fn find_module(&self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
3017 let mut result = None;
3018 let mut seen_modules = FxHashSet::default();
3019 let mut worklist = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self.r.graph_root.to_module(), ThinVec::new(), true)]))vec![(self.r.graph_root.to_module(), ThinVec::new(), true)];
3020
3021 while let Some((in_module, path_segments, doc_visible)) = worklist.pop() {
3022 if result.is_some() {
3024 break;
3025 }
3026
3027 in_module.for_each_child(self.r, |r, ident, orig_ident_span, _, name_binding| {
3028 if result.is_some() || !name_binding.vis().is_visible_locally() {
3030 return;
3031 }
3032 if let Some(module_def_id) = name_binding.res().module_like_def_id() {
3033 let mut path_segments = path_segments.clone();
3035 path_segments.push(ast::PathSegment::from_ident(ident.orig(orig_ident_span)));
3036 let doc_visible = doc_visible
3037 && (module_def_id.is_local() || !r.tcx.is_doc_hidden(module_def_id));
3038 if module_def_id == def_id {
3039 let path =
3040 Path { span: name_binding.span, segments: path_segments, tokens: None };
3041 result = Some((
3042 r.expect_module(module_def_id),
3043 ImportSuggestion {
3044 did: Some(def_id),
3045 descr: "module",
3046 path,
3047 accessible: true,
3048 doc_visible,
3049 note: None,
3050 via_import: false,
3051 is_stable: true,
3052 },
3053 ));
3054 } else {
3055 if seen_modules.insert(module_def_id) {
3057 let module = r.expect_module(module_def_id);
3058 worklist.push((module, path_segments, doc_visible));
3059 }
3060 }
3061 }
3062 });
3063 }
3064
3065 result
3066 }
3067
3068 fn collect_enum_ctors(&self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
3069 self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
3070 let mut variants = Vec::new();
3071 enum_module.for_each_child(self.r, |_, ident, orig_ident_span, _, name_binding| {
3072 if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
3073 let mut segms = enum_import_suggestion.path.segments.clone();
3074 segms.push(ast::PathSegment::from_ident(ident.orig(orig_ident_span)));
3075 let path = Path { span: name_binding.span, segments: segms, tokens: None };
3076 variants.push((path, def_id, kind));
3077 }
3078 });
3079 variants
3080 })
3081 }
3082
3083 fn suggest_using_enum_variant(
3085 &self,
3086 err: &mut Diag<'_>,
3087 source: PathSource<'_, '_, '_>,
3088 def_id: DefId,
3089 span: Span,
3090 ) {
3091 let Some(variant_ctors) = self.collect_enum_ctors(def_id) else {
3092 err.note("you might have meant to use one of the enum's variants");
3093 return;
3094 };
3095
3096 let (suggest_path_sep_dot_span, suggest_only_tuple_variants) = match source {
3101 PathSource::TupleStruct(..) => (None, true),
3103 PathSource::Expr(Some(expr)) => match &expr.kind {
3104 ExprKind::Call(..) => (None, true),
3106 ExprKind::MethodCall(MethodCall {
3109 receiver,
3110 span,
3111 seg: PathSegment { ident, .. },
3112 ..
3113 }) => {
3114 let dot_span = receiver.span.between(*span);
3115 let found_tuple_variant = variant_ctors.iter().any(|(path, _, ctor_kind)| {
3116 *ctor_kind == CtorKind::Fn
3117 && path.segments.last().is_some_and(|seg| seg.ident == *ident)
3118 });
3119 (found_tuple_variant.then_some(dot_span), false)
3120 }
3121 ExprKind::Field(base, ident) => {
3124 let dot_span = base.span.between(ident.span);
3125 let found_tuple_or_unit_variant = variant_ctors.iter().any(|(path, ..)| {
3126 path.segments.last().is_some_and(|seg| seg.ident == *ident)
3127 });
3128 (found_tuple_or_unit_variant.then_some(dot_span), false)
3129 }
3130 _ => (None, false),
3131 },
3132 _ => (None, false),
3133 };
3134
3135 if let Some(dot_span) = suggest_path_sep_dot_span {
3136 err.span_suggestion_verbose(
3137 dot_span,
3138 "use the path separator to refer to a variant",
3139 "::",
3140 Applicability::MaybeIncorrect,
3141 );
3142 } else if suggest_only_tuple_variants {
3143 let mut suggestable_variants = variant_ctors
3146 .iter()
3147 .filter(|(.., kind)| *kind == CtorKind::Fn)
3148 .map(|(variant, ..)| path_names_to_string(variant))
3149 .collect::<Vec<_>>();
3150 suggestable_variants.sort();
3151
3152 let non_suggestable_variant_count = variant_ctors.len() - suggestable_variants.len();
3153
3154 let source_msg = if #[allow(non_exhaustive_omitted_patterns)] match source {
PathSource::TupleStruct(..) => true,
_ => false,
}matches!(source, PathSource::TupleStruct(..)) {
3155 "to match against"
3156 } else {
3157 "to construct"
3158 };
3159
3160 if !suggestable_variants.is_empty() {
3161 let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
3162 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try {0} the enum\'s variant",
source_msg))
})format!("try {source_msg} the enum's variant")
3163 } else {
3164 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try {0} one of the enum\'s variants",
source_msg))
})format!("try {source_msg} one of the enum's variants")
3165 };
3166
3167 err.span_suggestions(
3168 span,
3169 msg,
3170 suggestable_variants,
3171 Applicability::MaybeIncorrect,
3172 );
3173 }
3174
3175 if non_suggestable_variant_count == variant_ctors.len() {
3177 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the enum has no tuple variants {0}",
source_msg))
})format!("the enum has no tuple variants {source_msg}"));
3178 }
3179
3180 if non_suggestable_variant_count == 1 {
3182 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant {0} the enum\'s non-tuple variant",
source_msg))
})format!("you might have meant {source_msg} the enum's non-tuple variant"));
3183 } else if non_suggestable_variant_count >= 1 {
3184 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant {0} one of the enum\'s non-tuple variants",
source_msg))
})format!(
3185 "you might have meant {source_msg} one of the enum's non-tuple variants"
3186 ));
3187 }
3188 } else {
3189 let needs_placeholder = |ctor_def_id: DefId, kind: CtorKind| {
3190 let def_id = self.r.tcx.parent(ctor_def_id);
3191 match kind {
3192 CtorKind::Const => false,
3193 CtorKind::Fn => {
3194 !self.r.field_idents(def_id).is_some_and(|field_ids| field_ids.is_empty())
3195 }
3196 }
3197 };
3198
3199 let mut suggestable_variants = variant_ctors
3200 .iter()
3201 .filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
3202 .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
3203 .map(|(variant, kind)| match kind {
3204 CtorKind::Const => variant,
3205 CtorKind::Fn => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}())", variant))
})format!("({variant}())"),
3206 })
3207 .collect::<Vec<_>>();
3208 suggestable_variants.sort();
3209 let no_suggestable_variant = suggestable_variants.is_empty();
3210
3211 if !no_suggestable_variant {
3212 let msg = if suggestable_variants.len() == 1 {
3213 "you might have meant to use the following enum variant"
3214 } else {
3215 "you might have meant to use one of the following enum variants"
3216 };
3217
3218 err.span_suggestions(
3219 span,
3220 msg,
3221 suggestable_variants,
3222 Applicability::MaybeIncorrect,
3223 );
3224 }
3225
3226 let mut suggestable_variants_with_placeholders = variant_ctors
3227 .iter()
3228 .filter(|(_, def_id, kind)| needs_placeholder(*def_id, *kind))
3229 .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
3230 .filter_map(|(variant, kind)| match kind {
3231 CtorKind::Fn => Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0}(/* fields */))", variant))
})format!("({variant}(/* fields */))")),
3232 _ => None,
3233 })
3234 .collect::<Vec<_>>();
3235 suggestable_variants_with_placeholders.sort();
3236
3237 if !suggestable_variants_with_placeholders.is_empty() {
3238 let msg =
3239 match (no_suggestable_variant, suggestable_variants_with_placeholders.len()) {
3240 (true, 1) => "the following enum variant is available",
3241 (true, _) => "the following enum variants are available",
3242 (false, 1) => "alternatively, the following enum variant is available",
3243 (false, _) => {
3244 "alternatively, the following enum variants are also available"
3245 }
3246 };
3247
3248 err.span_suggestions(
3249 span,
3250 msg,
3251 suggestable_variants_with_placeholders,
3252 Applicability::HasPlaceholders,
3253 );
3254 }
3255 };
3256
3257 if def_id.is_local() {
3258 err.span_note(self.r.def_span(def_id), "the enum is defined here");
3259 }
3260 }
3261
3262 pub(crate) fn detect_and_suggest_const_parameter_error(
3293 &mut self,
3294 path: &[Segment],
3295 source: PathSource<'_, 'ast, 'ra>,
3296 ) -> Option<Diag<'tcx>> {
3297 let Some(item) = self.diag_metadata.current_item else { return None };
3298 let ItemKind::Impl(impl_) = &item.kind else { return None };
3299 let self_ty = &impl_.self_ty;
3300
3301 let [current_parameter] = path else {
3303 return None;
3304 };
3305
3306 let target_ident = current_parameter.ident;
3307
3308 let visitor = ParentPathVisitor::new(self_ty, target_ident);
3310
3311 let Some(parent_segment) = visitor.parent else {
3312 return None;
3313 };
3314
3315 let Some(args) = parent_segment.args.as_ref() else {
3316 return None;
3317 };
3318
3319 let GenericArgs::AngleBracketed(angle) = args.as_ref() else {
3320 return None;
3321 };
3322
3323 let usage_to_pos: FxHashMap<NodeId, usize> = angle
3326 .args
3327 .iter()
3328 .enumerate()
3329 .filter_map(|(pos, arg)| {
3330 if let AngleBracketedArg::Arg(GenericArg::Type(ty)) = arg
3331 && let TyKind::Path(_, path) = &ty.kind
3332 && let [segment] = path.segments.as_slice()
3333 {
3334 Some((segment.id, pos))
3335 } else {
3336 None
3337 }
3338 })
3339 .collect();
3340
3341 let Some(idx) = current_parameter.id.and_then(|id| usage_to_pos.get(&id).copied()) else {
3344 return None;
3345 };
3346
3347 let ns = source.namespace();
3349 let segment = Segment::from(parent_segment);
3350 let segments = [segment];
3351 let finalize = Finalize::new(parent_segment.id, parent_segment.ident.span);
3352
3353 if let Ok(Some(resolve)) = self.resolve_qpath_anywhere(
3354 &None,
3355 &segments,
3356 ns,
3357 source.defer_to_typeck(),
3358 finalize,
3359 source,
3360 ) && let Some(resolve) = resolve.full_res()
3361 && let Res::Def(_, def_id) = resolve
3362 && def_id.is_local()
3363 && let Some(local_def_id) = def_id.as_local()
3364 && let Some(struct_generics) = self.r.struct_generics.get(&local_def_id)
3365 && let Some(target_param) = &struct_generics.params.get(idx)
3366 && let GenericParamKind::Const { ty, .. } = &target_param.kind
3367 && let TyKind::Path(_, path) = &ty.kind
3368 {
3369 let full_type = path
3370 .segments
3371 .iter()
3372 .map(|seg| seg.ident.to_string())
3373 .collect::<Vec<_>>()
3374 .join("::");
3375
3376 let next_impl_param = impl_.generics.params.iter().find(|impl_param| {
3381 angle
3382 .args
3383 .iter()
3384 .find_map(|arg| {
3385 if let AngleBracketedArg::Arg(GenericArg::Type(ty)) = arg
3386 && let TyKind::Path(_, path) = &ty.kind
3387 && let [segment] = path.segments.as_slice()
3388 && segment.ident == impl_param.ident
3389 {
3390 usage_to_pos.get(&segment.id).copied()
3391 } else {
3392 None
3393 }
3394 })
3395 .map_or(false, |pos| pos > idx)
3396 });
3397
3398 let (insert_span, snippet) = match next_impl_param {
3399 Some(next_param) => {
3400 (
3403 next_param.span().shrink_to_lo(),
3404 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("const {0}: {1}, ", target_ident,
full_type))
})format!("const {}: {}, ", target_ident, full_type),
3405 )
3406 }
3407 None => match impl_.generics.params.last() {
3408 Some(last) => {
3409 (
3412 last.span().shrink_to_hi(),
3413 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", const {0}: {1}", target_ident,
full_type))
})format!(", const {}: {}", target_ident, full_type),
3414 )
3415 }
3416 None => {
3417 (
3420 impl_.generics.span.shrink_to_hi(),
3421 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<const {0}: {1}>", target_ident,
full_type))
})format!("<const {}: {}>", target_ident, full_type),
3422 )
3423 }
3424 },
3425 };
3426
3427 let mut err = self.r.dcx().struct_span_err(
3428 target_ident.span,
3429 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot find const `{0}` in this scope",
target_ident))
})format!("cannot find const `{}` in this scope", target_ident),
3430 );
3431
3432 err.code(E0425);
3433
3434 err.span_label(target_ident.span, "not found in this scope");
3435
3436 err.span_label(
3437 target_param.span(),
3438 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("corresponding const parameter on the type defined here"))
})format!("corresponding const parameter on the type defined here",),
3439 );
3440
3441 err.subdiagnostic(errors::UnexpectedMissingConstParameter {
3442 span: insert_span,
3443 snippet,
3444 item_name: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", target_ident))
})format!("{}", target_ident),
3445 item_location: String::from("impl"),
3446 });
3447
3448 return Some(err);
3449 }
3450
3451 None
3452 }
3453
3454 pub(crate) fn suggest_adding_generic_parameter(
3455 &mut self,
3456 path: &[Segment],
3457 source: PathSource<'_, 'ast, 'ra>,
3458 ) -> (Option<(Span, &'static str, String, Applicability)>, Option<Diag<'tcx>>) {
3459 let (ident, span) = match path {
3460 [segment]
3461 if !segment.has_generic_args
3462 && segment.ident.name != kw::SelfUpper
3463 && segment.ident.name != kw::Dyn =>
3464 {
3465 (segment.ident.to_string(), segment.ident.span)
3466 }
3467 _ => return (None, None),
3468 };
3469 let mut iter = ident.chars().map(|c| c.is_uppercase());
3470 let single_uppercase_char =
3471 #[allow(non_exhaustive_omitted_patterns)] match iter.next() {
Some(true) => true,
_ => false,
}matches!(iter.next(), Some(true)) && #[allow(non_exhaustive_omitted_patterns)] match iter.next() {
None => true,
_ => false,
}matches!(iter.next(), None);
3472 if !self.diag_metadata.currently_processing_generic_args && !single_uppercase_char {
3473 return (None, None);
3474 }
3475 match (
3476 self.diag_metadata.current_item,
3477 single_uppercase_char,
3478 self.diag_metadata.currently_processing_generic_args,
3479 ) {
3480 (Some(Item { kind: ItemKind::Fn(fn_), .. }), _, _) if fn_.ident.name == sym::main => {
3481 }
3483 (
3484 Some(Item {
3485 kind:
3486 kind @ ItemKind::Fn(..)
3487 | kind @ ItemKind::Enum(..)
3488 | kind @ ItemKind::Struct(..)
3489 | kind @ ItemKind::Union(..),
3490 ..
3491 }),
3492 true,
3493 _,
3494 )
3495 | (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
3497 | (Some(Item { kind, .. }), false, _) => {
3498 if let Some(generics) = kind.generics() {
3499 if span.overlaps(generics.span) {
3500 return (None, None);
3509 }
3510
3511 let (msg, sugg) = match source {
3512 PathSource::Type | PathSource::PreciseCapturingArg(TypeNS) => {
3513 if let Some(err) =
3514 self.detect_and_suggest_const_parameter_error(path, source)
3515 {
3516 return (None, Some(err));
3517 }
3518 ("you might be missing a type parameter", ident)
3519 }
3520 PathSource::Expr(_) | PathSource::PreciseCapturingArg(ValueNS) => (
3521 "you might be missing a const parameter",
3522 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("const {0}: /* Type */", ident))
})format!("const {ident}: /* Type */"),
3523 ),
3524 _ => return (None, None),
3525 };
3526 let (span, sugg) = if let [.., param] = &generics.params[..] {
3527 let span = if let [.., bound] = ¶m.bounds[..] {
3528 bound.span()
3529 } else if let GenericParam {
3530 kind: GenericParamKind::Const { ty, span: _, default },
3531 ..
3532 } = param
3533 {
3534 default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
3535 } else {
3536 param.ident.span
3537 };
3538 (span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}", sugg))
})format!(", {sugg}"))
3539 } else {
3540 (generics.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", sugg))
})format!("<{sugg}>"))
3541 };
3542 if span.can_be_used_for_suggestions() {
3544 return (
3545 Some((span.shrink_to_hi(), msg, sugg, Applicability::MaybeIncorrect)),
3546 None,
3547 );
3548 }
3549 }
3550 }
3551 _ => {}
3552 }
3553 (None, None)
3554 }
3555
3556 pub(crate) fn suggestion_for_label_in_rib(
3559 &self,
3560 rib_index: usize,
3561 label: Ident,
3562 ) -> Option<LabelSuggestion> {
3563 let within_scope = self.is_label_valid_from_rib(rib_index);
3565
3566 let rib = &self.label_ribs[rib_index];
3567 let names = rib
3568 .bindings
3569 .iter()
3570 .filter(|(id, _)| id.span.eq_ctxt(label.span))
3571 .map(|(id, _)| id.name)
3572 .collect::<Vec<Symbol>>();
3573
3574 find_best_match_for_name(&names, label.name, None).map(|symbol| {
3575 let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
3579 (*ident, within_scope)
3580 })
3581 }
3582
3583 pub(crate) fn maybe_report_lifetime_uses(
3584 &mut self,
3585 generics_span: Span,
3586 params: &[ast::GenericParam],
3587 ) {
3588 for (param_index, param) in params.iter().enumerate() {
3589 let GenericParamKind::Lifetime = param.kind else { continue };
3590
3591 let def_id = self.r.local_def_id(param.id);
3592
3593 let use_set = self.lifetime_uses.remove(&def_id);
3594 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3594",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(3594u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Use set for {0:?}({1:?} at {2:?}) is {3:?}",
def_id, param.ident, param.ident.span, use_set) as
&dyn Value))])
});
} else { ; }
};debug!(
3595 "Use set for {:?}({:?} at {:?}) is {:?}",
3596 def_id, param.ident, param.ident.span, use_set
3597 );
3598
3599 let deletion_span = || {
3600 if params.len() == 1 {
3601 Some(generics_span)
3603 } else if param_index == 0 {
3604 match (
3607 param.span().find_ancestor_inside(generics_span),
3608 params[param_index + 1].span().find_ancestor_inside(generics_span),
3609 ) {
3610 (Some(param_span), Some(next_param_span)) => {
3611 Some(param_span.to(next_param_span.shrink_to_lo()))
3612 }
3613 _ => None,
3614 }
3615 } else {
3616 match (
3619 param.span().find_ancestor_inside(generics_span),
3620 params[param_index - 1].span().find_ancestor_inside(generics_span),
3621 ) {
3622 (Some(param_span), Some(prev_param_span)) => {
3623 Some(prev_param_span.shrink_to_hi().to(param_span))
3624 }
3625 _ => None,
3626 }
3627 }
3628 };
3629 match use_set {
3630 Some(LifetimeUseSet::Many) => {}
3631 Some(LifetimeUseSet::One { .. }) if !param.bounds.is_empty() => {}
3634 Some(LifetimeUseSet::One { use_span, use_ctxt }) => {
3635 let param_ident = param.ident;
3636 let deletion_span =
3637 if param.bounds.is_empty() { deletion_span() } else { None };
3638 self.r.lint_buffer.dyn_buffer_lint_any(
3639 lint::builtin::SINGLE_USE_LIFETIMES,
3640 param.id,
3641 param_ident.span,
3642 move |dcx, level, sess| {
3643 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3643",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(3643u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["param_ident",
"param_ident.span", "use_span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(¶m_ident)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(¶m_ident.span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&use_span)
as &dyn Value))])
});
} else { ; }
};debug!(?param_ident, ?param_ident.span, ?use_span);
3644
3645 let elidable = #[allow(non_exhaustive_omitted_patterns)] match use_ctxt {
LifetimeCtxt::Ref => true,
_ => false,
}matches!(use_ctxt, LifetimeCtxt::Ref);
3646 let suggestion = if let Some(deletion_span) = deletion_span {
3647 let (use_span, replace_lt) = if elidable {
3648 let use_span = sess
3649 .downcast_ref::<Session>()
3650 .expect("expected a `Session`")
3651 .source_map()
3652 .span_extend_while_whitespace(use_span);
3653 (use_span, String::new())
3654 } else {
3655 (use_span, "'_".to_owned())
3656 };
3657 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3657",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(3657u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["deletion_span",
"use_span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&deletion_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&use_span)
as &dyn Value))])
});
} else { ; }
};debug!(?deletion_span, ?use_span);
3658
3659 let deletion_span = if deletion_span.is_empty() {
3662 None
3663 } else {
3664 Some(deletion_span)
3665 };
3666 Some(errors::SingleUseLifetimeSugg {
3667 deletion_span,
3668 use_span,
3669 replace_lt,
3670 })
3671 } else {
3672 None
3673 };
3674 errors::SingleUseLifetime {
3675 suggestion,
3676 param_span: param_ident.span,
3677 use_span,
3678 ident: param_ident,
3679 }
3680 .into_diag(dcx, level)
3681 },
3682 );
3683 }
3684 None => {
3685 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:3685",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(3685u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["param.ident",
"param.ident.span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(¶m.ident)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(¶m.ident.span)
as &dyn Value))])
});
} else { ; }
};debug!(?param.ident, ?param.ident.span);
3686 let deletion_span = deletion_span();
3687
3688 if deletion_span.is_some_and(|sp| !sp.in_derive_expansion()) {
3690 self.r.lint_buffer.buffer_lint(
3691 lint::builtin::UNUSED_LIFETIMES,
3692 param.id,
3693 param.ident.span,
3694 errors::UnusedLifetime { deletion_span, ident: param.ident },
3695 );
3696 }
3697 }
3698 }
3699 }
3700 }
3701
3702 pub(crate) fn emit_undeclared_lifetime_error(
3703 &self,
3704 lifetime_ref: &ast::Lifetime,
3705 outer_lifetime_ref: Option<Ident>,
3706 ) -> ErrorGuaranteed {
3707 if true {
match (&lifetime_ref.ident.name, &kw::UnderscoreLifetime) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_ne!(lifetime_ref.ident.name, kw::UnderscoreLifetime);
3708 let mut err = if let Some(outer) = outer_lifetime_ref {
3709 {
self.r.dcx().struct_span_err(lifetime_ref.ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("can\'t use generic parameters from outer item"))
})).with_code(E0401)
}struct_span_code_err!(
3710 self.r.dcx(),
3711 lifetime_ref.ident.span,
3712 E0401,
3713 "can't use generic parameters from outer item",
3714 )
3715 .with_span_label(lifetime_ref.ident.span, "use of generic parameter from outer item")
3716 .with_span_label(outer.span, "lifetime parameter from outer item")
3717 } else {
3718 {
self.r.dcx().struct_span_err(lifetime_ref.ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use of undeclared lifetime name `{0}`",
lifetime_ref.ident))
})).with_code(E0261)
}struct_span_code_err!(
3719 self.r.dcx(),
3720 lifetime_ref.ident.span,
3721 E0261,
3722 "use of undeclared lifetime name `{}`",
3723 lifetime_ref.ident
3724 )
3725 .with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
3726 };
3727
3728 if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
3730 err.span_suggestion_verbose(
3731 lifetime_ref.ident.span,
3732 "you may have misspelled the `'static` lifetime",
3733 "'static",
3734 Applicability::MachineApplicable,
3735 );
3736 } else {
3737 self.suggest_introducing_lifetime(
3738 &mut err,
3739 Some(lifetime_ref.ident),
3740 |err, _, span, message, suggestion, span_suggs| {
3741 err.multipart_suggestion(
3742 message,
3743 std::iter::once((span, suggestion)).chain(span_suggs).collect(),
3744 Applicability::MaybeIncorrect,
3745 );
3746 true
3747 },
3748 );
3749 }
3750
3751 err.emit()
3752 }
3753
3754 fn suggest_introducing_lifetime(
3755 &self,
3756 err: &mut Diag<'_>,
3757 name: Option<Ident>,
3758 suggest: impl Fn(
3759 &mut Diag<'_>,
3760 bool,
3761 Span,
3762 Cow<'static, str>,
3763 String,
3764 Vec<(Span, String)>,
3765 ) -> bool,
3766 ) {
3767 let mut suggest_note = true;
3768 for rib in self.lifetime_ribs.iter().rev() {
3769 let mut should_continue = true;
3770 match rib.kind {
3771 LifetimeRibKind::Generics { binder, span, kind } => {
3772 if let LifetimeBinderKind::ConstItem = kind
3775 && !self.r.tcx().features().generic_const_items()
3776 {
3777 continue;
3778 }
3779 if let LifetimeBinderKind::ImplAssocType = kind {
3780 continue;
3781 }
3782
3783 if !span.can_be_used_for_suggestions()
3784 && suggest_note
3785 && let Some(name) = name
3786 {
3787 suggest_note = false; err.span_label(
3789 span,
3790 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` is missing in item created through this procedural macro",
name))
})format!(
3791 "lifetime `{name}` is missing in item created through this procedural macro",
3792 ),
3793 );
3794 continue;
3795 }
3796
3797 let higher_ranked = #[allow(non_exhaustive_omitted_patterns)] match kind {
LifetimeBinderKind::FnPtrType | LifetimeBinderKind::PolyTrait |
LifetimeBinderKind::WhereBound => true,
_ => false,
}matches!(
3798 kind,
3799 LifetimeBinderKind::FnPtrType
3800 | LifetimeBinderKind::PolyTrait
3801 | LifetimeBinderKind::WhereBound
3802 );
3803
3804 let mut rm_inner_binders: FxIndexSet<Span> = Default::default();
3805 let (span, sugg) = if span.is_empty() {
3806 let mut binder_idents: FxIndexSet<Ident> = Default::default();
3807 binder_idents.insert(name.unwrap_or(Ident::from_str("'a")));
3808
3809 if let LifetimeBinderKind::WhereBound = kind
3816 && let Some(predicate) = self.diag_metadata.current_where_predicate
3817 && let ast::WherePredicateKind::BoundPredicate(
3818 ast::WhereBoundPredicate { bounded_ty, bounds, .. },
3819 ) = &predicate.kind
3820 && bounded_ty.id == binder
3821 {
3822 for bound in bounds {
3823 if let ast::GenericBound::Trait(poly_trait_ref) = bound
3824 && let span = poly_trait_ref
3825 .span
3826 .with_hi(poly_trait_ref.trait_ref.path.span.lo())
3827 && !span.is_empty()
3828 {
3829 rm_inner_binders.insert(span);
3830 poly_trait_ref.bound_generic_params.iter().for_each(|v| {
3831 binder_idents.insert(v.ident);
3832 });
3833 }
3834 }
3835 }
3836
3837 let binders_sugg: String = binder_idents
3838 .into_iter()
3839 .map(|ident| ident.to_string())
3840 .intersperse(", ".to_owned())
3841 .collect();
3842 let sugg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}<{1}>{2}",
if higher_ranked { "for" } else { "" }, binders_sugg,
if higher_ranked { " " } else { "" }))
})format!(
3843 "{}<{}>{}",
3844 if higher_ranked { "for" } else { "" },
3845 binders_sugg,
3846 if higher_ranked { " " } else { "" },
3847 );
3848 (span, sugg)
3849 } else {
3850 let span = self
3851 .r
3852 .tcx
3853 .sess
3854 .source_map()
3855 .span_through_char(span, '<')
3856 .shrink_to_hi();
3857 let sugg =
3858 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}, ",
name.map(|i| i.to_string()).as_deref().unwrap_or("'a")))
})format!("{}, ", name.map(|i| i.to_string()).as_deref().unwrap_or("'a"));
3859 (span, sugg)
3860 };
3861
3862 if higher_ranked {
3863 let message = Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider making the {0} lifetime-generic with a new `{1}` lifetime",
kind.descr(),
name.map(|i| i.to_string()).as_deref().unwrap_or("'a")))
})format!(
3864 "consider making the {} lifetime-generic with a new `{}` lifetime",
3865 kind.descr(),
3866 name.map(|i| i.to_string()).as_deref().unwrap_or("'a"),
3867 ));
3868 should_continue = suggest(
3869 err,
3870 true,
3871 span,
3872 message,
3873 sugg,
3874 if !rm_inner_binders.is_empty() {
3875 rm_inner_binders
3876 .into_iter()
3877 .map(|v| (v, "".to_string()))
3878 .collect::<Vec<_>>()
3879 } else {
3880 ::alloc::vec::Vec::new()vec![]
3881 },
3882 );
3883 err.note_once(
3884 "for more information on higher-ranked polymorphism, visit \
3885 https://doc.rust-lang.org/nomicon/hrtb.html",
3886 );
3887 } else if let Some(name) = name {
3888 let message =
3889 Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider introducing lifetime `{0}` here",
name))
})format!("consider introducing lifetime `{name}` here"));
3890 should_continue = suggest(err, false, span, message, sugg, ::alloc::vec::Vec::new()vec![]);
3891 } else {
3892 let message = Cow::from("consider introducing a named lifetime parameter");
3893 should_continue = suggest(err, false, span, message, sugg, ::alloc::vec::Vec::new()vec![]);
3894 }
3895 }
3896 LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy => break,
3897 _ => {}
3898 }
3899 if !should_continue {
3900 break;
3901 }
3902 }
3903 }
3904
3905 pub(crate) fn emit_non_static_lt_in_const_param_ty_error(
3906 &self,
3907 lifetime_ref: &ast::Lifetime,
3908 ) -> ErrorGuaranteed {
3909 self.r
3910 .dcx()
3911 .create_err(errors::ParamInTyOfConstParam {
3912 span: lifetime_ref.ident.span,
3913 name: lifetime_ref.ident.name,
3914 })
3915 .emit()
3916 }
3917
3918 pub(crate) fn emit_forbidden_non_static_lifetime_error(
3922 &self,
3923 cause: NoConstantGenericsReason,
3924 lifetime_ref: &ast::Lifetime,
3925 ) -> ErrorGuaranteed {
3926 match cause {
3927 NoConstantGenericsReason::IsEnumDiscriminant => self
3928 .r
3929 .dcx()
3930 .create_err(errors::ParamInEnumDiscriminant {
3931 span: lifetime_ref.ident.span,
3932 name: lifetime_ref.ident.name,
3933 param_kind: errors::ParamKindInEnumDiscriminant::Lifetime,
3934 })
3935 .emit(),
3936 NoConstantGenericsReason::NonTrivialConstArg => {
3937 if !!self.r.tcx.features().generic_const_exprs() {
::core::panicking::panic("assertion failed: !self.r.tcx.features().generic_const_exprs()")
};assert!(!self.r.tcx.features().generic_const_exprs());
3938 self.r
3939 .dcx()
3940 .create_err(errors::ParamInNonTrivialAnonConst {
3941 span: lifetime_ref.ident.span,
3942 name: lifetime_ref.ident.name,
3943 param_kind: errors::ParamKindInNonTrivialAnonConst::Lifetime,
3944 help: self.r.tcx.sess.is_nightly_build(),
3945 is_gca: self.r.tcx.features().generic_const_args(),
3946 help_gca: self.r.tcx.features().generic_const_args(),
3947 })
3948 .emit()
3949 }
3950 }
3951 }
3952
3953 pub(crate) fn report_missing_lifetime_specifiers<'a>(
3954 &mut self,
3955 lifetime_refs: impl Clone + IntoIterator<Item = &'a MissingLifetime>,
3956 function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
3957 ) -> ErrorGuaranteed {
3958 let num_lifetimes: usize = lifetime_refs.clone().into_iter().map(|lt| lt.count).sum();
3959 let spans: Vec<_> = lifetime_refs.clone().into_iter().map(|lt| lt.span).collect();
3960
3961 let mut err = {
self.r.dcx().struct_span_err(spans,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("missing lifetime specifier{0}",
if num_lifetimes == 1 { "" } else { "s" }))
})).with_code(E0106)
}struct_span_code_err!(
3962 self.r.dcx(),
3963 spans,
3964 E0106,
3965 "missing lifetime specifier{}",
3966 pluralize!(num_lifetimes)
3967 );
3968 self.add_missing_lifetime_specifiers_label(
3969 &mut err,
3970 lifetime_refs,
3971 function_param_lifetimes,
3972 );
3973 err.emit()
3974 }
3975
3976 fn add_missing_lifetime_specifiers_label<'a>(
3977 &mut self,
3978 err: &mut Diag<'_>,
3979 lifetime_refs: impl Clone + IntoIterator<Item = &'a MissingLifetime>,
3980 function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
3981 ) {
3982 for < in lifetime_refs.clone() {
3983 err.span_label(
3984 lt.span,
3985 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0} lifetime parameter{1}",
if lt.count == 1 {
"named".to_string()
} else { lt.count.to_string() },
if lt.count == 1 { "" } else { "s" }))
})format!(
3986 "expected {} lifetime parameter{}",
3987 if lt.count == 1 { "named".to_string() } else { lt.count.to_string() },
3988 pluralize!(lt.count),
3989 ),
3990 );
3991 }
3992
3993 let mut in_scope_lifetimes: Vec<_> = self
3994 .lifetime_ribs
3995 .iter()
3996 .rev()
3997 .take_while(|rib| {
3998 !#[allow(non_exhaustive_omitted_patterns)] match rib.kind {
LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy => true,
_ => false,
}matches!(rib.kind, LifetimeRibKind::Item | LifetimeRibKind::ConstParamTy)
3999 })
4000 .flat_map(|rib| rib.bindings.iter())
4001 .map(|(&ident, &res)| (ident, res))
4002 .filter(|(ident, _)| ident.name != kw::UnderscoreLifetime)
4003 .collect();
4004 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4004",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(4004u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["in_scope_lifetimes"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&in_scope_lifetimes)
as &dyn Value))])
});
} else { ; }
};debug!(?in_scope_lifetimes);
4005
4006 let mut maybe_static = false;
4007 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4007",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(4007u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["function_param_lifetimes"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&function_param_lifetimes)
as &dyn Value))])
});
} else { ; }
};debug!(?function_param_lifetimes);
4008 if let Some((param_lifetimes, params)) = &function_param_lifetimes {
4009 let elided_len = param_lifetimes.len();
4010 let num_params = params.len();
4011
4012 let mut m = String::new();
4013
4014 for (i, info) in params.iter().enumerate() {
4015 let ElisionFnParameter { ident, index, lifetime_count, span } = *info;
4016 if true {
match (&lifetime_count, &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_ne!(lifetime_count, 0);
4017
4018 err.span_label(span, "");
4019
4020 if i != 0 {
4021 if i + 1 < num_params {
4022 m.push_str(", ");
4023 } else if num_params == 2 {
4024 m.push_str(" or ");
4025 } else {
4026 m.push_str(", or ");
4027 }
4028 }
4029
4030 let help_name = if let Some(ident) = ident {
4031 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", ident))
})format!("`{ident}`")
4032 } else {
4033 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("argument {0}", index + 1))
})format!("argument {}", index + 1)
4034 };
4035
4036 if lifetime_count == 1 {
4037 m.push_str(&help_name[..])
4038 } else {
4039 m.push_str(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("one of {0}\'s {1} lifetimes",
help_name, lifetime_count))
})format!("one of {help_name}'s {lifetime_count} lifetimes")[..])
4040 }
4041 }
4042
4043 if num_params == 0 {
4044 err.help(
4045 "this function's return type contains a borrowed value, but there is no value \
4046 for it to be borrowed from",
4047 );
4048 if in_scope_lifetimes.is_empty() {
4049 maybe_static = true;
4050 in_scope_lifetimes = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(Ident::with_dummy_span(kw::StaticLifetime),
(DUMMY_NODE_ID, LifetimeRes::Static))]))vec![(
4051 Ident::with_dummy_span(kw::StaticLifetime),
4052 (DUMMY_NODE_ID, LifetimeRes::Static),
4053 )];
4054 }
4055 } else if elided_len == 0 {
4056 err.help(
4057 "this function's return type contains a borrowed value with an elided \
4058 lifetime, but the lifetime cannot be derived from the arguments",
4059 );
4060 if in_scope_lifetimes.is_empty() {
4061 maybe_static = true;
4062 in_scope_lifetimes = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(Ident::with_dummy_span(kw::StaticLifetime),
(DUMMY_NODE_ID, LifetimeRes::Static))]))vec![(
4063 Ident::with_dummy_span(kw::StaticLifetime),
4064 (DUMMY_NODE_ID, LifetimeRes::Static),
4065 )];
4066 }
4067 } else if num_params == 1 {
4068 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this function\'s return type contains a borrowed value, but the signature does not say which {0} it is borrowed from",
m))
})format!(
4069 "this function's return type contains a borrowed value, but the signature does \
4070 not say which {m} it is borrowed from",
4071 ));
4072 } else {
4073 err.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this function\'s return type contains a borrowed value, but the signature does not say whether it is borrowed from {0}",
m))
})format!(
4074 "this function's return type contains a borrowed value, but the signature does \
4075 not say whether it is borrowed from {m}",
4076 ));
4077 }
4078 }
4079
4080 #[allow(rustc::symbol_intern_string_literal)]
4081 let existing_name = match &in_scope_lifetimes[..] {
4082 [] => Symbol::intern("'a"),
4083 [(existing, _)] => existing.name,
4084 _ => Symbol::intern("'lifetime"),
4085 };
4086
4087 let mut spans_suggs: Vec<_> = Vec::new();
4088 let source_map = self.r.tcx.sess.source_map();
4089 let build_sugg = |lt: MissingLifetime| match lt.kind {
4090 MissingLifetimeKind::Underscore => {
4091 if true {
match (<.count, &1) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(lt.count, 1);
4092 (lt.span, existing_name.to_string())
4093 }
4094 MissingLifetimeKind::Ampersand => {
4095 if true {
match (<.count, &1) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(lt.count, 1);
4096 (lt.span.shrink_to_hi(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ", existing_name))
})format!("{existing_name} "))
4097 }
4098 MissingLifetimeKind::Comma => {
4099 let sugg: String = std::iter::repeat_n(existing_name.as_str(), lt.count)
4100 .intersperse(", ")
4101 .collect();
4102 let is_empty_brackets = source_map.span_followed_by(lt.span, ">").is_some();
4103 let sugg = if is_empty_brackets { sugg } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}, ", sugg))
})format!("{sugg}, ") };
4104 (lt.span.shrink_to_hi(), sugg)
4105 }
4106 MissingLifetimeKind::Brackets => {
4107 let sugg: String = std::iter::once("<")
4108 .chain(std::iter::repeat_n(existing_name.as_str(), lt.count).intersperse(", "))
4109 .chain([">"])
4110 .collect();
4111 (lt.span.shrink_to_hi(), sugg)
4112 }
4113 };
4114 for < in lifetime_refs.clone() {
4115 spans_suggs.push(build_sugg(lt));
4116 }
4117 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_resolve/src/late/diagnostics.rs:4117",
"rustc_resolve::late::diagnostics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_resolve/src/late/diagnostics.rs"),
::tracing_core::__macro_support::Option::Some(4117u32),
::tracing_core::__macro_support::Option::Some("rustc_resolve::late::diagnostics"),
::tracing_core::field::FieldSet::new(&["spans_suggs"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&spans_suggs)
as &dyn Value))])
});
} else { ; }
};debug!(?spans_suggs);
4118 match in_scope_lifetimes.len() {
4119 0 => {
4120 if let Some((param_lifetimes, _)) = function_param_lifetimes {
4121 for lt in param_lifetimes {
4122 spans_suggs.push(build_sugg(lt))
4123 }
4124 }
4125 self.suggest_introducing_lifetime(
4126 err,
4127 None,
4128 |err, higher_ranked, span, message, intro_sugg, _| {
4129 err.multipart_suggestion(
4130 message,
4131 std::iter::once((span, intro_sugg))
4132 .chain(spans_suggs.clone())
4133 .collect(),
4134 Applicability::MaybeIncorrect,
4135 );
4136 higher_ranked
4137 },
4138 );
4139 }
4140 1 => {
4141 let post = if maybe_static {
4142 let mut lifetime_refs = lifetime_refs.clone().into_iter();
4143 let owned = if let Some(lt) = lifetime_refs.next()
4144 && lifetime_refs.next().is_none()
4145 && lt.kind != MissingLifetimeKind::Ampersand
4146 {
4147 ", or if you will only have owned values"
4148 } else {
4149 ""
4150 };
4151 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", but this is uncommon unless you\'re returning a borrowed value from a `const` or a `static`{0}",
owned))
})format!(
4152 ", but this is uncommon unless you're returning a borrowed value from a \
4153 `const` or a `static`{owned}",
4154 )
4155 } else {
4156 String::new()
4157 };
4158 err.multipart_suggestion(
4159 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider using the `{0}` lifetime{1}",
existing_name, post))
})format!("consider using the `{existing_name}` lifetime{post}"),
4160 spans_suggs,
4161 Applicability::MaybeIncorrect,
4162 );
4163 if maybe_static {
4164 let mut lifetime_refs = lifetime_refs.clone().into_iter();
4170 if let Some(lt) = lifetime_refs.next()
4171 && lifetime_refs.next().is_none()
4172 && (lt.kind == MissingLifetimeKind::Ampersand
4173 || lt.kind == MissingLifetimeKind::Underscore)
4174 {
4175 let pre = if let Some((kind, _span)) = self.diag_metadata.current_function
4176 && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4177 && !sig.decl.inputs.is_empty()
4178 && let sugg = sig
4179 .decl
4180 .inputs
4181 .iter()
4182 .filter_map(|param| {
4183 if param.ty.span.contains(lt.span) {
4184 None
4187 } else if let TyKind::CVarArgs = param.ty.kind {
4188 None
4190 } else if let TyKind::ImplTrait(..) = ¶m.ty.kind {
4191 None
4193 } else {
4194 Some((param.ty.span.shrink_to_lo(), "&".to_string()))
4195 }
4196 })
4197 .collect::<Vec<_>>()
4198 && !sugg.is_empty()
4199 {
4200 let (the, s) = if sig.decl.inputs.len() == 1 {
4201 ("the", "")
4202 } else {
4203 ("one of the", "s")
4204 };
4205 let dotdotdot =
4206 if lt.kind == MissingLifetimeKind::Ampersand { "..." } else { "" };
4207 err.multipart_suggestion(
4208 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("instead, you are more likely to want to change {0} argument{1} to be borrowed{2}",
the, s, dotdotdot))
})format!(
4209 "instead, you are more likely to want to change {the} \
4210 argument{s} to be borrowed{dotdotdot}",
4211 ),
4212 sugg,
4213 Applicability::MaybeIncorrect,
4214 );
4215 "...or alternatively, you might want"
4216 } else if (lt.kind == MissingLifetimeKind::Ampersand
4217 || lt.kind == MissingLifetimeKind::Underscore)
4218 && let Some((kind, _span)) = self.diag_metadata.current_function
4219 && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4220 && let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output
4221 && !sig.decl.inputs.is_empty()
4222 && let arg_refs = sig
4223 .decl
4224 .inputs
4225 .iter()
4226 .filter_map(|param| match ¶m.ty.kind {
4227 TyKind::ImplTrait(_, bounds) => Some(bounds),
4228 _ => None,
4229 })
4230 .flat_map(|bounds| bounds.into_iter())
4231 .collect::<Vec<_>>()
4232 && !arg_refs.is_empty()
4233 {
4234 let mut lt_finder =
4240 LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4241 for bound in arg_refs {
4242 if let ast::GenericBound::Trait(trait_ref) = bound {
4243 lt_finder.visit_trait_ref(&trait_ref.trait_ref);
4244 }
4245 }
4246 lt_finder.visit_ty(ret_ty);
4247 let spans_suggs: Vec<_> = lt_finder
4248 .seen
4249 .iter()
4250 .filter_map(|ty| match &ty.kind {
4251 TyKind::Ref(_, mut_ty) => {
4252 let span = ty.span.with_hi(mut_ty.ty.span.lo());
4253 Some((span, "&'a ".to_string()))
4254 }
4255 _ => None,
4256 })
4257 .collect();
4258 self.suggest_introducing_lifetime(
4259 err,
4260 None,
4261 |err, higher_ranked, span, message, intro_sugg, _| {
4262 err.multipart_suggestion(
4263 message,
4264 std::iter::once((span, intro_sugg))
4265 .chain(spans_suggs.clone())
4266 .collect(),
4267 Applicability::MaybeIncorrect,
4268 );
4269 higher_ranked
4270 },
4271 );
4272 "alternatively, you might want"
4273 } else {
4274 "instead, you are more likely to want"
4275 };
4276 let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
4277 let mut sugg_is_str_to_string = false;
4278 let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(lt.span, String::new())]))vec![(lt.span, String::new())];
4279 if let Some((kind, _span)) = self.diag_metadata.current_function
4280 && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4281 {
4282 let mut lt_finder =
4283 LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4284 for param in &sig.decl.inputs {
4285 lt_finder.visit_ty(¶m.ty);
4286 }
4287 if let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output {
4288 lt_finder.visit_ty(ret_ty);
4289 let mut ret_lt_finder =
4290 LifetimeFinder { lifetime: lt.span, found: None, seen: ::alloc::vec::Vec::new()vec![] };
4291 ret_lt_finder.visit_ty(ret_ty);
4292 if let [Ty { span, kind: TyKind::Ref(_, mut_ty), .. }] =
4293 &ret_lt_finder.seen[..]
4294 {
4295 sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.with_hi(mut_ty.ty.span.lo()), String::new())]))vec![(span.with_hi(mut_ty.ty.span.lo()), String::new())];
4301 owned_sugg = true;
4302 }
4303 }
4304 if let Some(ty) = lt_finder.found {
4305 if let TyKind::Path(None, path) = &ty.kind {
4306 let path: Vec<_> = Segment::from_path(path);
4308 match self.resolve_path(
4309 &path,
4310 Some(TypeNS),
4311 None,
4312 PathSource::Type,
4313 ) {
4314 PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
4315 match module.res() {
4316 Some(Res::PrimTy(PrimTy::Str)) => {
4317 sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(lt.span.with_hi(ty.span.hi()), "String".to_string())]))vec![(
4319 lt.span.with_hi(ty.span.hi()),
4320 "String".to_string(),
4321 )];
4322 sugg_is_str_to_string = true;
4323 }
4324 Some(Res::PrimTy(..)) => {}
4325 Some(Res::Def(
4326 DefKind::Struct
4327 | DefKind::Union
4328 | DefKind::Enum
4329 | DefKind::ForeignTy
4330 | DefKind::AssocTy
4331 | DefKind::OpaqueTy
4332 | DefKind::TyParam,
4333 _,
4334 )) => {}
4335 _ => {
4336 owned_sugg = false;
4338 }
4339 }
4340 }
4341 PathResult::NonModule(res) => {
4342 match res.base_res() {
4343 Res::PrimTy(PrimTy::Str) => {
4344 sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(lt.span.with_hi(ty.span.hi()), "String".to_string())]))vec![(
4346 lt.span.with_hi(ty.span.hi()),
4347 "String".to_string(),
4348 )];
4349 sugg_is_str_to_string = true;
4350 }
4351 Res::PrimTy(..) => {}
4352 Res::Def(
4353 DefKind::Struct
4354 | DefKind::Union
4355 | DefKind::Enum
4356 | DefKind::ForeignTy
4357 | DefKind::AssocTy
4358 | DefKind::OpaqueTy
4359 | DefKind::TyParam,
4360 _,
4361 ) => {}
4362 _ => {
4363 owned_sugg = false;
4365 }
4366 }
4367 }
4368 _ => {
4369 owned_sugg = false;
4371 }
4372 }
4373 }
4374 if let TyKind::Slice(inner_ty) = &ty.kind {
4375 sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
(ty.span.with_lo(inner_ty.span.hi()), ">".to_string())]))vec![
4377 (lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
4378 (ty.span.with_lo(inner_ty.span.hi()), ">".to_string()),
4379 ];
4380 }
4381 }
4382 }
4383 if owned_sugg {
4384 if let Some(span) =
4385 self.find_ref_prefix_span_for_owned_suggestion(lt.span)
4386 && !sugg_is_str_to_string
4387 {
4388 sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span, String::new())]))vec![(span, String::new())];
4389 }
4390 err.multipart_suggestion(
4391 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} to return an owned value",
pre))
})format!("{pre} to return an owned value"),
4392 sugg,
4393 Applicability::MaybeIncorrect,
4394 );
4395 }
4396 }
4397 }
4398 }
4399 _ => {
4400 let lifetime_spans: Vec<_> =
4401 in_scope_lifetimes.iter().map(|(ident, _)| ident.span).collect();
4402 err.span_note(lifetime_spans, "these named lifetimes are available to use");
4403
4404 if spans_suggs.len() > 0 {
4405 err.multipart_suggestion(
4408 "consider using one of the available lifetimes here",
4409 spans_suggs,
4410 Applicability::HasPlaceholders,
4411 );
4412 }
4413 }
4414 }
4415 }
4416
4417 fn find_ref_prefix_span_for_owned_suggestion(&self, lifetime: Span) -> Option<Span> {
4418 let mut finder = RefPrefixSpanFinder { lifetime, span: None };
4419 if let Some(item) = self.diag_metadata.current_item {
4420 finder.visit_item(item);
4421 } else if let Some((kind, _span)) = self.diag_metadata.current_function
4422 && let FnKind::Fn(_, _, ast::Fn { sig, .. }) = kind
4423 {
4424 for param in &sig.decl.inputs {
4425 finder.visit_ty(¶m.ty);
4426 }
4427 if let ast::FnRetTy::Ty(ret_ty) = &sig.decl.output {
4428 finder.visit_ty(ret_ty);
4429 }
4430 }
4431 finder.span
4432 }
4433}
4434
4435fn mk_where_bound_predicate(
4436 path: &Path,
4437 poly_trait_ref: &ast::PolyTraitRef,
4438 ty: &Ty,
4439) -> Option<ast::WhereBoundPredicate> {
4440 let modified_segments = {
4441 let mut segments = path.segments.clone();
4442 let [preceding @ .., second_last, last] = segments.as_mut_slice() else {
4443 return None;
4444 };
4445 let mut segments = ThinVec::from(preceding);
4446
4447 let added_constraint = ast::AngleBracketedArg::Constraint(ast::AssocItemConstraint {
4448 id: DUMMY_NODE_ID,
4449 ident: last.ident,
4450 gen_args: None,
4451 kind: ast::AssocItemConstraintKind::Equality {
4452 term: ast::Term::Ty(Box::new(ast::Ty {
4453 kind: ast::TyKind::Path(None, poly_trait_ref.trait_ref.path.clone()),
4454 id: DUMMY_NODE_ID,
4455 span: DUMMY_SP,
4456 tokens: None,
4457 })),
4458 },
4459 span: DUMMY_SP,
4460 });
4461
4462 match second_last.args.as_deref_mut() {
4463 Some(ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs { args, .. })) => {
4464 args.push(added_constraint);
4465 }
4466 Some(_) => return None,
4467 None => {
4468 second_last.args =
4469 Some(Box::new(ast::GenericArgs::AngleBracketed(ast::AngleBracketedArgs {
4470 args: ThinVec::from([added_constraint]),
4471 span: DUMMY_SP,
4472 })));
4473 }
4474 }
4475
4476 segments.push(second_last.clone());
4477 segments
4478 };
4479
4480 let new_where_bound_predicate = ast::WhereBoundPredicate {
4481 bound_generic_params: ThinVec::new(),
4482 bounded_ty: Box::new(ty.clone()),
4483 bounds: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[ast::GenericBound::Trait(ast::PolyTraitRef {
bound_generic_params: ThinVec::new(),
modifiers: ast::TraitBoundModifiers::NONE,
trait_ref: ast::TraitRef {
path: ast::Path {
segments: modified_segments,
span: DUMMY_SP,
tokens: None,
},
ref_id: DUMMY_NODE_ID,
},
span: DUMMY_SP,
parens: ast::Parens::No,
})]))vec![ast::GenericBound::Trait(ast::PolyTraitRef {
4484 bound_generic_params: ThinVec::new(),
4485 modifiers: ast::TraitBoundModifiers::NONE,
4486 trait_ref: ast::TraitRef {
4487 path: ast::Path { segments: modified_segments, span: DUMMY_SP, tokens: None },
4488 ref_id: DUMMY_NODE_ID,
4489 },
4490 span: DUMMY_SP,
4491 parens: ast::Parens::No,
4492 })],
4493 };
4494
4495 Some(new_where_bound_predicate)
4496}
4497
4498pub(super) fn signal_lifetime_shadowing(
4500 sess: &Session,
4501 orig: Ident,
4502 shadower: Ident,
4503) -> ErrorGuaranteed {
4504 {
sess.dcx().struct_span_err(shadower.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime name `{0}` shadows a lifetime name that is already in scope",
orig.name))
})).with_code(E0496)
}struct_span_code_err!(
4505 sess.dcx(),
4506 shadower.span,
4507 E0496,
4508 "lifetime name `{}` shadows a lifetime name that is already in scope",
4509 orig.name,
4510 )
4511 .with_span_label(orig.span, "first declared here")
4512 .with_span_label(shadower.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` already in scope",
orig.name))
})format!("lifetime `{}` already in scope", orig.name))
4513 .emit()
4514}
4515
4516struct LifetimeFinder<'ast> {
4517 lifetime: Span,
4518 found: Option<&'ast Ty>,
4519 seen: Vec<&'ast Ty>,
4520}
4521
4522impl<'ast> Visitor<'ast> for LifetimeFinder<'ast> {
4523 fn visit_ty(&mut self, t: &'ast Ty) {
4524 if let TyKind::Ref(_, mut_ty) | TyKind::PinnedRef(_, mut_ty) = &t.kind {
4525 self.seen.push(t);
4526 if t.span.lo() == self.lifetime.lo() {
4527 self.found = Some(&mut_ty.ty);
4528 }
4529 }
4530 walk_ty(self, t)
4531 }
4532}
4533
4534struct RefPrefixSpanFinder {
4535 lifetime: Span,
4536 span: Option<Span>,
4537}
4538
4539impl<'ast> Visitor<'ast> for RefPrefixSpanFinder {
4540 fn visit_ty(&mut self, t: &'ast Ty) {
4541 if self.span.is_some() {
4542 return;
4543 }
4544 if let TyKind::Ref(_, mut_ty) | TyKind::PinnedRef(_, mut_ty) = &t.kind
4545 && t.span.lo() == self.lifetime.lo()
4546 {
4547 self.span = Some(t.span.with_hi(mut_ty.ty.span.lo()));
4548 return;
4549 }
4550 walk_ty(self, t);
4551 }
4552}
4553
4554pub(super) fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
4557 let name = shadower.name;
4558 let shadower = shadower.span;
4559 sess.dcx()
4560 .struct_span_warn(
4561 shadower,
4562 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("label name `{0}` shadows a label name that is already in scope",
name))
})format!("label name `{name}` shadows a label name that is already in scope"),
4563 )
4564 .with_span_label(orig, "first declared here")
4565 .with_span_label(shadower, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("label `{0}` already in scope",
name))
})format!("label `{name}` already in scope"))
4566 .emit();
4567}
4568
4569struct ParentPathVisitor<'a> {
4570 target: Ident,
4571 parent: Option<&'a PathSegment>,
4572 stack: Vec<&'a Ty>,
4573}
4574
4575impl<'a> ParentPathVisitor<'a> {
4576 fn new(self_ty: &'a Ty, target: Ident) -> Self {
4577 let mut v = ParentPathVisitor { target, parent: None, stack: Vec::new() };
4578
4579 v.visit_ty(self_ty);
4580 v
4581 }
4582}
4583
4584impl<'a> Visitor<'a> for ParentPathVisitor<'a> {
4585 fn visit_ty(&mut self, ty: &'a Ty) {
4586 if self.parent.is_some() {
4587 return;
4588 }
4589
4590 self.stack.push(ty);
4592
4593 if let TyKind::Path(_, path) = &ty.kind
4594 && let [segment] = path.segments.as_slice()
4596 && segment.ident == self.target
4597 && let [.., parent_ty, _ty] = self.stack.as_slice()
4599 && let TyKind::Path(_, parent_path) = &parent_ty.kind
4600 {
4601 self.parent = parent_path.segments.first();
4602 }
4603
4604 walk_ty(self, ty);
4605
4606 self.stack.pop();
4607 }
4608}