Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ pub enum StashKey {
/// FRU syntax
MaybeFruTypo,
CallAssocMethod,
AssociatedTypeSuggestion,
UndeterminedMacroResolution,
/// Used by `Parser::maybe_recover_trailing_expr`
ExprInPat,
Expand Down
21 changes: 9 additions & 12 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use rustc_ast::TraitObjectSyntax;
use rustc_attr_ir::lang_items::LangItem;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, Diagnostic, Level, StashKey, Suggestions,
struct_span_code_err,
};
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, Level, struct_span_code_err};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, HirId};
Expand All @@ -17,6 +14,7 @@ use rustc_middle::ty::{
};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::{ErrorGuaranteed, Span};
use rustc_trait_selection::diagnostics::AssocTypeWithSameName;
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
use rustc_trait_selection::traits;
Expand Down Expand Up @@ -655,15 +653,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
poly_trait_ref.trait_ref.trait_def_id(),
&mut diag,
);
// In case there is an associated type with the same name
// Add the suggestion to this error
if let Some(mut sugg) =
self.dcx().steal_non_err(span, StashKey::AssociatedTypeSuggestion)
&& let Suggestions::Enabled(ref mut s1) = diag.suggestions
&& let Suggestions::Enabled(ref mut s2) = sugg.suggestions
// If there is an associated type with the same name, add the suggestion to this error.
if self
.tcx()
.resolutions(())
.paths_matching_assoc_types
.contains(&span.with_parent(None))
{
s1.append(s2);
sugg.cancel();
diag.subdiagnostic(AssocTypeWithSameName { span: span.shrink_to_lo() });
}
Some(diag.emit_err())
} else {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ pub struct ResolverGlobalCtxt {
/// A list of proc macro LocalDefIds, written out in the order in which
/// they are declared in the static array generated by proc_macro_harness.
pub proc_macros: Vec<LocalDefId>,
/// Path spans where the ident matches an associated type for an enclosing trait. Used to
/// improve certain error messages.
pub paths_matching_assoc_types: UnordSet<Span>,
/// Mapping from ident span to path span for paths that don't exist as written, but that
/// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
pub confused_type_with_std_module: FxIndexMap<Span, Span>,
Expand Down
13 changes: 3 additions & 10 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagArgValue, Diagnostic, ErrorGuaranteed, IntoDiagArg, MultiSpan,
StashKey, Suggestions, elided_lifetime_in_path_suggestion, pluralize,
Suggestions, elided_lifetime_in_path_suggestion, pluralize,
};
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{CtorKind, DefKind, NonMacroAttrKind, PerNS};
Expand Down Expand Up @@ -4823,7 +4823,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
source,
) {
Ok(Some(partial_res)) if let Some(res) = partial_res.full_res() => {
// if we also have an associated type that matches the ident, stash a suggestion
// If we also have an associated type that matches the ident, record that.
if let Some(items) = self.diag_metadata.current_trait_assoc_items
&& let [Segment { ident, .. }] = path
&& items.iter().any(|item| {
Expand All @@ -4836,14 +4836,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
})
{
let mut diag = self.r.tcx.dcx().struct_allow("");
diag.span_suggestion_verbose(
path_span.shrink_to_lo(),
"there is an associated type with the same name",
"Self::",
Applicability::MaybeIncorrect,
);
diag.stash(path_span, StashKey::AssociatedTypeSuggestion);
self.r.paths_matching_assoc_types.insert(path_span.with_parent(None));
}

if source.is_expected(res) || res == Res::Err {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ pub struct Resolver<'ra, 'tcx> {
/// A list of proc macro LocalDefIds, written out in the order in which
/// they are declared in the static array generated by proc_macro_harness.
proc_macros: Vec<LocalDefId> = Vec::new(),
paths_matching_assoc_types: UnordSet<Span>,
confused_type_with_std_module: FxIndexMap<Span, Span>,

/// Names of items that were stripped out via cfg with their corresponding cfg meta item.
Expand Down Expand Up @@ -1892,6 +1893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
invocation_parents,
trait_impls: Default::default(),
confused_type_with_std_module: Default::default(),
paths_matching_assoc_types: Default::default(),
stripped_cfg_items: Default::default(),
effective_visibilities: Default::default(),
macro_reachable_adts: Default::default(),
Expand Down Expand Up @@ -1970,6 +1972,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let glob_map = self.glob_map;
let main_def = self.main_def;
let confused_type_with_std_module = self.confused_type_with_std_module;
let paths_matching_assoc_types = self.paths_matching_assoc_types;
let effective_visibilities = self.effective_visibilities;

let stripped_cfg_items = self
Expand Down Expand Up @@ -2000,6 +2003,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
trait_impls: self.trait_impls,
proc_macros,
confused_type_with_std_module,
paths_matching_assoc_types,
doc_link_resolutions: self.doc_link_resolutions,
doc_link_traits_in_scope: self.doc_link_traits_in_scope,
all_macro_rules: self.all_macro_rules,
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_trait_selection/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2083,3 +2083,15 @@ pub(crate) struct NonGenericOpaqueTypeParam<'a, 'tcx> {
}")]
pub param_span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
"there is an associated type with the same name",
style = "verbose",
code = "Self::",
applicability = "maybe-incorrect"
)]
pub struct AssocTypeWithSameName {
#[primary_span]
pub span: Span,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, StringPart, Sublevel, Suggestions,
msg, pluralize, struct_span_code_err,
Applicability, Diag, ErrorGuaranteed, MultiSpan, StringPart, Sublevel, msg, pluralize,
struct_span_code_err,
};
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
use rustc_hir::intravisit::Visitor;
Expand Down Expand Up @@ -42,7 +42,8 @@ use tracing::{debug, instrument};
use super::suggestions::get_explanation_based_on_obligation;
use super::{ArgKind, CandidateSimilarity, GetSafeTransmuteErrorAndReason, ImplCandidate};
use crate::diagnostics::{
ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, CoroClosureNotFn,
AssocTypeWithSameName, ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch,
CoroClosureNotFn,
};
use crate::error_reporting::TypeErrCtxt;
use crate::error_reporting::infer::TyCategory;
Expand Down Expand Up @@ -3107,13 +3108,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);
self.suggest_unsized_bound_if_applicable(err, obligation);
if let Some(span) = err.span.primary_span()
&& let Some(mut diag) =
self.dcx().steal_non_err(span, StashKey::AssociatedTypeSuggestion)
&& let Suggestions::Enabled(ref mut s1) = err.suggestions
&& let Suggestions::Enabled(ref mut s2) = diag.suggestions
&& self
.tcx
.resolutions(())
.paths_matching_assoc_types
.contains(&span.with_parent(None))
{
s1.append(s2);
diag.cancel()
err.subdiagnostic(AssocTypeWithSameName { span: span.shrink_to_lo() });
}
}
}
Expand Down
Loading