Skip to content

Commit

Permalink
review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 13, 2023
1 parent 20c622e commit 2cfe8ed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
use rustc_middle::mir::{self, CallSource};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::TraitRef;
use rustc_middle::ty::{suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, Ty};
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
use rustc_middle::ty::{
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef,
Param, TraitRef, Ty, TyParamContext,
};
use rustc_middle::util::{call_kind, CallDesugaringKind, CallKind};
use rustc_session::parse::feature_err;
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -129,10 +130,9 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
tcx,
generics,
err,
&param_ty.name.as_str(),
TyParamContext(param_ty.name.as_str(), None),
&constraint,
None,
None,
);
}
}
Expand Down
17 changes: 8 additions & 9 deletions compiler/rustc_hir_analysis/src/astconv/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::errors::{
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::def_id::DefId;
use rustc_infer::traits::FulfillmentError;
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TyCtxt};
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TyCtxt, TyParamContext};
use rustc_session::parse::feature_err;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::symbol::{sym, Ident};
Expand Down Expand Up @@ -100,8 +100,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
pub(crate) fn complain_about_assoc_type_not_found<I>(
&self,
all_candidates: impl Fn() -> I,
ty_param_name: &str,
ty_param_def_id: Option<LocalDefId>,
ty_param: TyParamContext<'_>,
assoc_name: Ident,
span: Span,
) -> ErrorGuaranteed
Expand All @@ -118,7 +117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
E0220,
"associated type `{}` not found for `{}`",
assoc_name,
ty_param_name
ty_param.0,
);

if is_dummy {
Expand Down Expand Up @@ -200,7 +199,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
),
);
let hir = self.tcx().hir();
if let Some(def_id) = ty_param_def_id
if let Some(def_id) = ty_param.1
&& let Some(def_id) = def_id.as_local()
&& let parent = hir.get_parent_item(hir.local_def_id_to_hir_id(def_id))
&& let Some(generics) = hir.get_generics(parent.def_id)
{
Expand Down Expand Up @@ -228,10 +228,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.tcx(),
generics,
&mut err,
&ty_param_name,
ty_param,
&trait_name,
None,
None,
)
&& suggested_name != assoc_name.name
{
Expand Down Expand Up @@ -263,7 +262,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

err.span_suggestion(
assoc_name.span,
format!("`{ty_param_name}` has the following associated type"),
format!("`{}` has the following associated type", ty_param.0),
all_candidate_names.first().unwrap().to_string(),
applicability,
);
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_middle::middle::stability::AllowUnstable;
use rustc_middle::ty::GenericParamDefKind;
use rustc_middle::ty::{
self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, Ty, TyCtxt, TypeVisitableExt,
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
TyParamContext, TypeVisitableExt,
};
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
use rustc_span::edit_distance::find_best_match_for_name;
Expand Down Expand Up @@ -1096,8 +1096,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
(None, None) => {
let reported = self.complain_about_assoc_type_not_found(
all_candidates,
&ty_param_name.to_string(),
ty_param_def_id,
TyParamContext(
&ty_param_name.to_string(),
ty_param_def_id.map(|id| id.to_def_id()),
),
assoc_name,
span,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::print::Printer;
use rustc_middle::{
traits::ObligationCause,
ty::{self, error::TypeError, print::FmtPrinter, suggest_constraining_type_param, Ty},
ty::{
self, error::TypeError, print::FmtPrinter, suggest_constraining_type_param, Ty,
TyParamContext,
},
};
use rustc_span::{def_id::DefId, sym, BytePos, Span, Symbol};

Expand Down Expand Up @@ -139,9 +142,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
tcx,
generics,
diag,
&proj.self_ty().to_string(),
TyParamContext(&proj.self_ty().to_string(), None),
&path,
None,
matching_span,
);
} else {
Expand All @@ -153,9 +155,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
tcx,
generics,
diag,
&proj.self_ty().to_string(),
TyParamContext(&proj.self_ty().to_string(), None),
&path,
None,
matching_span,
);
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ impl<'tcx> IntoDiagnosticArg for Ty<'tcx> {
}
}

pub struct TyParamContext<'a>(pub &'a str, pub Option<DefId>);

impl<'tcx> Ty<'tcx> {
/// Similar to `Ty::is_primitive`, but also considers inferred numeric values to be primitive.
pub fn is_primitive_ty(self) -> bool {
Expand Down Expand Up @@ -216,16 +218,15 @@ pub fn suggest_constraining_type_param(
tcx: TyCtxt<'_>,
generics: &hir::Generics<'_>,
err: &mut Diagnostic,
param_name: &str,
ty_param: TyParamContext<'_>,
constraint: &str,
def_id: Option<DefId>,
span_to_replace: Option<Span>,
) -> bool {
suggest_constraining_type_params(
tcx,
generics,
err,
[(param_name, constraint, def_id)].into_iter(),
[(ty_param.0, constraint, ty_param.1)].into_iter(),
span_to_replace,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use rustc_middle::hir::map;
use rustc_middle::ty::error::TypeError::{self, Sorts};
use rustc_middle::ty::{
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, GenericArgs,
InferTy, IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder,
TypeSuperFoldable, TypeVisitableExt, TypeckResults,
InferTy, IsSuggestable, ToPredicate, Ty, TyCtxt, TyParamContext, TypeAndMut, TypeFoldable,
TypeFolder, TypeSuperFoldable, TypeVisitableExt, TypeckResults,
};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{sym, Ident, Symbol};
Expand Down Expand Up @@ -649,9 +649,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.tcx,
generics,
&mut err,
&param_name,
TyParamContext(&param_name, Some(trait_pred.def_id())),
&constraint,
Some(trait_pred.def_id()),
None,
) {
return;
Expand Down Expand Up @@ -1102,9 +1101,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.tcx,
generics,
err,
param.name.as_str(),
TyParamContext(param.name.as_str(), Some(clone_trait)),
"Clone",
Some(clone_trait),
None,
);
}
Expand Down

0 comments on commit 2cfe8ed

Please sign in to comment.