Automatically taint when reporting errors from ItemCtxt
This commit is contained in:
parent
cd3d98b3be
commit
fd9a92542c
26 changed files with 183 additions and 374 deletions
|
@ -18,7 +18,9 @@ use rustc_ast::Recovered;
|
|||
use rustc_data_structures::captures::Captures;
|
||||
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
|
||||
use rustc_data_structures::unord::UnordMap;
|
||||
use rustc_errors::{struct_span_code_err, Applicability, Diag, ErrorGuaranteed, StashKey, E0228};
|
||||
use rustc_errors::{
|
||||
struct_span_code_err, Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, StashKey, E0228,
|
||||
};
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_hir::intravisit::{self, walk_generics, Visitor};
|
||||
|
@ -370,6 +372,10 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
|
|||
self.tcx
|
||||
}
|
||||
|
||||
fn dcx(&self) -> DiagCtxtHandle<'_> {
|
||||
self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
|
||||
}
|
||||
|
||||
fn item_def_id(&self) -> LocalDefId {
|
||||
self.item_def_id
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
|
||||
if unbounds.len() > 1 {
|
||||
tcx.dcx().emit_err(errors::MultipleRelaxedDefaultBounds {
|
||||
self.dcx().emit_err(errors::MultipleRelaxedDefaultBounds {
|
||||
spans: unbounds.iter().map(|ptr| ptr.span).collect(),
|
||||
});
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
continue;
|
||||
}
|
||||
// There was a `?Trait` bound, but it was not `?Sized`; warn.
|
||||
tcx.dcx().span_warn(
|
||||
self.dcx().span_warn(
|
||||
unbound.span,
|
||||
"relaxing a default bound only does something for `?Sized`; \
|
||||
all other traits are not bound by default",
|
||||
|
@ -310,7 +310,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
duplicates
|
||||
.entry(assoc_item.def_id)
|
||||
.and_modify(|prev_span| {
|
||||
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
|
||||
self.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
|
||||
span: constraint.span,
|
||||
prev_span: *prev_span,
|
||||
item_name: constraint.ident,
|
||||
|
@ -338,7 +338,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
.into(),
|
||||
ty::GenericParamDefKind::Type { .. } => {
|
||||
let guar = *emitted_bad_param_err.get_or_insert_with(|| {
|
||||
tcx.dcx().emit_err(
|
||||
self.dcx().emit_err(
|
||||
crate::errors::ReturnTypeNotationIllegalParam::Type {
|
||||
span: path_span,
|
||||
param_span: tcx.def_span(param.def_id),
|
||||
|
@ -349,7 +349,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
ty::GenericParamDefKind::Const { .. } => {
|
||||
let guar = *emitted_bad_param_err.get_or_insert_with(|| {
|
||||
tcx.dcx().emit_err(
|
||||
self.dcx().emit_err(
|
||||
crate::errors::ReturnTypeNotationIllegalParam::Const {
|
||||
span: path_span,
|
||||
param_span: tcx.def_span(param.def_id),
|
||||
|
@ -371,7 +371,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
{
|
||||
alias_ty.into()
|
||||
} else {
|
||||
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit {
|
||||
return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit {
|
||||
span: constraint.span,
|
||||
ty: tcx.liberate_late_bound_regions(assoc_item.def_id, output),
|
||||
fn_span: tcx.hir().span_if_local(assoc_item.def_id),
|
||||
|
@ -417,7 +417,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let ty = alias_term
|
||||
.map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args));
|
||||
let ty =
|
||||
check_assoc_const_binding_type(tcx, constraint.ident, ty, constraint.hir_id);
|
||||
check_assoc_const_binding_type(self, constraint.ident, ty, constraint.hir_id);
|
||||
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
|
||||
}
|
||||
|
||||
|
@ -426,7 +426,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
match constraint.kind {
|
||||
hir::AssocItemConstraintKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
|
||||
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
|
||||
return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
|
||||
span: constraint.span,
|
||||
}));
|
||||
}
|
||||
|
@ -462,7 +462,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
late_bound_in_term,
|
||||
|br_name| {
|
||||
struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
constraint.span,
|
||||
E0582,
|
||||
"binding for associated type `{}` references {}, \
|
||||
|
@ -519,7 +519,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
///
|
||||
/// [^1]: <https://github.com/rust-lang/project-const-generics/issues/28>.
|
||||
fn check_assoc_const_binding_type<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cx: &dyn HirTyLowerer<'tcx>,
|
||||
assoc_const: Ident,
|
||||
ty: ty::Binder<'tcx, Ty<'tcx>>,
|
||||
hir_id: hir::HirId,
|
||||
|
@ -536,13 +536,14 @@ fn check_assoc_const_binding_type<'tcx>(
|
|||
}
|
||||
|
||||
let mut collector = GenericParamAndBoundVarCollector {
|
||||
tcx,
|
||||
cx,
|
||||
params: Default::default(),
|
||||
vars: Default::default(),
|
||||
depth: ty::INNERMOST,
|
||||
};
|
||||
let mut guar = ty.visit_with(&mut collector).break_value();
|
||||
|
||||
let tcx = cx.tcx();
|
||||
let ty_note = ty
|
||||
.make_suggestable(tcx, false, None)
|
||||
.map(|ty| crate::errors::TyOfAssocConstBindingNote { assoc_const, ty });
|
||||
|
@ -556,7 +557,7 @@ fn check_assoc_const_binding_type<'tcx>(
|
|||
for index in collector.params {
|
||||
let param = generics.param_at(index as _, tcx);
|
||||
let is_self_param = param.name == rustc_span::symbol::kw::SelfUpper;
|
||||
guar.get_or_insert(tcx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
|
||||
guar.get_or_insert(cx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
|
||||
span: assoc_const.span,
|
||||
assoc_const,
|
||||
param_name: param.name,
|
||||
|
@ -574,7 +575,7 @@ fn check_assoc_const_binding_type<'tcx>(
|
|||
}));
|
||||
}
|
||||
for (var_def_id, var_name) in collector.vars {
|
||||
guar.get_or_insert(tcx.dcx().emit_err(
|
||||
guar.get_or_insert(cx.dcx().emit_err(
|
||||
crate::errors::EscapingBoundVarInTyOfAssocConstBinding {
|
||||
span: assoc_const.span,
|
||||
assoc_const,
|
||||
|
@ -590,14 +591,14 @@ fn check_assoc_const_binding_type<'tcx>(
|
|||
Ty::new_error(tcx, guar)
|
||||
}
|
||||
|
||||
struct GenericParamAndBoundVarCollector<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
struct GenericParamAndBoundVarCollector<'a, 'tcx> {
|
||||
cx: &'a dyn HirTyLowerer<'tcx>,
|
||||
params: FxIndexSet<u32>,
|
||||
vars: FxIndexSet<(DefId, Symbol)>,
|
||||
depth: ty::DebruijnIndex,
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx> {
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 'tcx> {
|
||||
type Result = ControlFlow<ErrorGuaranteed>;
|
||||
|
||||
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
|
||||
|
@ -620,7 +621,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
|
|||
ty::BoundTyKind::Param(def_id, name) => (def_id, name),
|
||||
ty::BoundTyKind::Anon => {
|
||||
let reported = self
|
||||
.tcx
|
||||
.cx
|
||||
.dcx()
|
||||
.delayed_bug(format!("unexpected anon bound ty: {:?}", bt.var));
|
||||
return ControlFlow::Break(reported);
|
||||
|
@ -643,7 +644,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
|
|||
ty::BrNamed(def_id, name) => (def_id, name),
|
||||
ty::BrAnon | ty::BrEnv => {
|
||||
let guar = self
|
||||
.tcx
|
||||
.cx
|
||||
.dcx()
|
||||
.delayed_bug(format!("unexpected bound region kind: {:?}", br.kind));
|
||||
return ControlFlow::Break(guar);
|
||||
|
@ -661,7 +662,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
|
|||
self.params.insert(param.index);
|
||||
}
|
||||
ty::ConstKind::Bound(db, ty::BoundVar { .. }) if db >= self.depth => {
|
||||
let guar = self.tcx.dcx().delayed_bug("unexpected escaping late-bound const var");
|
||||
let guar = self.cx.dcx().delayed_bug("unexpected escaping late-bound const var");
|
||||
return ControlFlow::Break(guar);
|
||||
}
|
||||
_ if ct.has_param() || ct.has_bound_vars() => return ct.super_visit_with(self),
|
||||
|
|
|
@ -46,7 +46,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
return;
|
||||
}
|
||||
|
||||
self.tcx().dcx().emit_err(MissingTypeParams {
|
||||
self.dcx().emit_err(MissingTypeParams {
|
||||
span,
|
||||
def_span: self.tcx().def_span(def_id),
|
||||
span_snippet: self.tcx().sess.source_map().span_to_snippet(span).ok(),
|
||||
|
@ -109,7 +109,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
if is_impl {
|
||||
let trait_name = self.tcx().def_path_str(trait_def_id);
|
||||
self.tcx().dcx().emit_err(ManualImplementation { span, trait_name });
|
||||
self.dcx().emit_err(ManualImplementation { span, trait_name });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
if is_dummy {
|
||||
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span });
|
||||
return tcx.dcx().emit_err(err);
|
||||
return self.dcx().emit_err(err);
|
||||
}
|
||||
|
||||
let all_candidate_names: Vec<_> = all_candidates()
|
||||
|
@ -174,7 +174,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
assoc_kind: assoc_kind_str,
|
||||
suggested_name,
|
||||
});
|
||||
return tcx.dcx().emit_err(err);
|
||||
return self.dcx().emit_err(err);
|
||||
}
|
||||
|
||||
// If we didn't find a good item in the supertraits (or couldn't get
|
||||
|
@ -239,10 +239,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
assoc_kind: assoc_kind_str,
|
||||
suggested_name,
|
||||
});
|
||||
return tcx.dcx().emit_err(err);
|
||||
return self.dcx().emit_err(err);
|
||||
}
|
||||
|
||||
let mut err = tcx.dcx().create_err(err);
|
||||
let mut err = self.dcx().create_err(err);
|
||||
if suggest_constraining_type_param(
|
||||
tcx,
|
||||
generics,
|
||||
|
@ -264,7 +264,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
return err.emit();
|
||||
}
|
||||
return tcx.dcx().emit_err(err);
|
||||
return self.dcx().emit_err(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -291,7 +291,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span: assoc_name.span });
|
||||
}
|
||||
|
||||
tcx.dcx().emit_err(err)
|
||||
self.dcx().emit_err(err)
|
||||
}
|
||||
|
||||
fn complain_about_assoc_kind_mismatch(
|
||||
|
@ -347,7 +347,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
(ident.span, None, assoc_kind, assoc_item.kind)
|
||||
};
|
||||
|
||||
tcx.dcx().emit_err(errors::AssocKindMismatch {
|
||||
self.dcx().emit_err(errors::AssocKindMismatch {
|
||||
span,
|
||||
expected: super::assoc_kind_str(expected),
|
||||
got: super::assoc_kind_str(got),
|
||||
|
@ -366,8 +366,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
traits: &[String],
|
||||
name: Symbol,
|
||||
) -> ErrorGuaranteed {
|
||||
let mut err =
|
||||
struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
|
||||
let mut err = struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type");
|
||||
if self
|
||||
.tcx()
|
||||
.resolutions(())
|
||||
|
@ -475,7 +474,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
span: Span,
|
||||
) -> ErrorGuaranteed {
|
||||
let mut err = struct_span_code_err!(
|
||||
self.tcx().dcx(),
|
||||
self.dcx(),
|
||||
name.span,
|
||||
E0034,
|
||||
"multiple applicable items in scope"
|
||||
|
@ -576,7 +575,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
};
|
||||
|
||||
let mut err = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
name.span,
|
||||
E0220,
|
||||
"associated type `{name}` not found for `{self_ty}` in the current scope"
|
||||
|
@ -662,7 +661,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
bounds.sort();
|
||||
bounds.dedup();
|
||||
|
||||
let mut err = tcx.dcx().struct_span_err(
|
||||
let mut err = self.dcx().struct_span_err(
|
||||
name.span,
|
||||
format!("the associated type `{name}` exists for `{self_ty}`, but its trait bounds were not satisfied")
|
||||
);
|
||||
|
@ -829,7 +828,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
trait_bound_spans.sort();
|
||||
let mut err = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
trait_bound_spans,
|
||||
E0191,
|
||||
"the value of the associated type{} {} must be specified",
|
||||
|
@ -1012,7 +1011,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
.next()
|
||||
{
|
||||
let reported =
|
||||
struct_span_code_err!(tcx.dcx(), span, E0223, "ambiguous associated type")
|
||||
struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type")
|
||||
.with_span_suggestion_verbose(
|
||||
ident2.span.to(ident3.span),
|
||||
format!("there is an associated function with a similar name: `{name}`"),
|
||||
|
@ -1120,7 +1119,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let last_span = *arg_spans.last().unwrap();
|
||||
let span: MultiSpan = arg_spans.into();
|
||||
let mut err = struct_span_code_err!(
|
||||
self.tcx().dcx(),
|
||||
self.dcx(),
|
||||
span,
|
||||
E0109,
|
||||
"{kind} arguments are not allowed on {this_type}",
|
||||
|
@ -1139,11 +1138,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
&self,
|
||||
regular_traits: &Vec<TraitAliasExpansionInfo<'_>>,
|
||||
) -> ErrorGuaranteed {
|
||||
let tcx = self.tcx();
|
||||
let first_trait = ®ular_traits[0];
|
||||
let additional_trait = ®ular_traits[1];
|
||||
let mut err = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
additional_trait.bottom().1,
|
||||
E0225,
|
||||
"only auto traits can be used as additional traits in a trait object"
|
||||
|
@ -1186,7 +1184,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
.find(|&trait_ref| tcx.is_trait_alias(trait_ref))
|
||||
.map(|trait_ref| tcx.def_span(trait_ref));
|
||||
let reported =
|
||||
tcx.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
|
||||
self.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
|
||||
self.set_tainted_by_errors(reported);
|
||||
reported
|
||||
}
|
||||
|
@ -1194,11 +1192,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
/// Emit an error for the given associated item constraint.
|
||||
pub fn prohibit_assoc_item_constraint(
|
||||
tcx: TyCtxt<'_>,
|
||||
cx: &dyn HirTyLowerer<'_>,
|
||||
constraint: &hir::AssocItemConstraint<'_>,
|
||||
segment: Option<(DefId, &hir::PathSegment<'_>, Span)>,
|
||||
) -> ErrorGuaranteed {
|
||||
let mut err = tcx.dcx().create_err(AssocItemConstraintsNotAllowedHere {
|
||||
let tcx = cx.tcx();
|
||||
let mut err = cx.dcx().create_err(AssocItemConstraintsNotAllowedHere {
|
||||
span: constraint.span,
|
||||
fn_trait_expansion: if let Some((_, segment, span)) = segment
|
||||
&& segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::IsMethodCall;
|
||||
use super::{HirTyLowerer, IsMethodCall};
|
||||
use crate::errors::wrong_number_of_generic_args::{GenericArgsInfo, WrongNumberOfGenericArgs};
|
||||
use crate::hir_ty_lowering::{
|
||||
errors::prohibit_assoc_item_constraint, ExplicitLateBound, GenericArgCountMismatch,
|
||||
|
@ -13,7 +13,7 @@ use rustc_hir::def::{DefKind, Res};
|
|||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::GenericArg;
|
||||
use rustc_middle::ty::{
|
||||
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
|
||||
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty,
|
||||
};
|
||||
use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS;
|
||||
use rustc_span::symbol::{kw, sym};
|
||||
|
@ -22,15 +22,16 @@ use smallvec::SmallVec;
|
|||
/// Report an error that a generic argument did not match the generic parameter that was
|
||||
/// expected.
|
||||
fn generic_arg_mismatch_err(
|
||||
tcx: TyCtxt<'_>,
|
||||
cx: &dyn HirTyLowerer<'_>,
|
||||
arg: &GenericArg<'_>,
|
||||
param: &GenericParamDef,
|
||||
possible_ordering_error: bool,
|
||||
help: Option<String>,
|
||||
) -> ErrorGuaranteed {
|
||||
let tcx = cx.tcx();
|
||||
let sess = tcx.sess;
|
||||
let mut err = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
cx.dcx(),
|
||||
arg.span(),
|
||||
E0747,
|
||||
"{} provided when a {} was expected",
|
||||
|
@ -171,7 +172,7 @@ fn generic_arg_mismatch_err(
|
|||
/// - `inferred_kind`: if no parameter was provided, and inference
|
||||
/// is enabled, then creates a suitable inference variable.
|
||||
pub fn lower_generic_args<'tcx: 'a, 'a>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
cx: &dyn HirTyLowerer<'tcx>,
|
||||
def_id: DefId,
|
||||
parent_args: &[ty::GenericArg<'tcx>],
|
||||
has_self: bool,
|
||||
|
@ -179,6 +180,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
|
|||
arg_count: &GenericArgCountResult,
|
||||
ctx: &mut impl GenericArgsLowerer<'a, 'tcx>,
|
||||
) -> GenericArgsRef<'tcx> {
|
||||
let tcx = cx.tcx();
|
||||
// Collect the segments of the path; we need to instantiate arguments
|
||||
// for parameters throughout the entire path (wherever there are
|
||||
// generic parameters).
|
||||
|
@ -326,7 +328,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
|
|||
param_types_present.dedup();
|
||||
|
||||
generic_arg_mismatch_err(
|
||||
tcx,
|
||||
cx,
|
||||
arg,
|
||||
param,
|
||||
!args_iter.clone().is_sorted_by_key(|arg| arg.to_ord()),
|
||||
|
@ -381,7 +383,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
|
|||
assert_eq!(kind, "lifetime");
|
||||
let (provided_arg, param) =
|
||||
force_infer_lt.expect("lifetimes ought to have been inferred");
|
||||
generic_arg_mismatch_err(tcx, provided_arg, param, false, None);
|
||||
generic_arg_mismatch_err(cx, provided_arg, param, false, None);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -405,7 +407,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
|
|||
/// Checks that the correct number of generic arguments have been provided.
|
||||
/// Used specifically for function calls.
|
||||
pub fn check_generic_arg_count_for_call(
|
||||
tcx: TyCtxt<'_>,
|
||||
cx: &dyn HirTyLowerer<'_>,
|
||||
def_id: DefId,
|
||||
generics: &ty::Generics,
|
||||
seg: &hir::PathSegment<'_>,
|
||||
|
@ -416,14 +418,14 @@ pub fn check_generic_arg_count_for_call(
|
|||
IsMethodCall::No => GenericArgPosition::Value,
|
||||
};
|
||||
let has_self = generics.parent.is_none() && generics.has_self;
|
||||
check_generic_arg_count(tcx, def_id, seg, generics, gen_pos, has_self)
|
||||
check_generic_arg_count(cx, def_id, seg, generics, gen_pos, has_self)
|
||||
}
|
||||
|
||||
/// Checks that the correct number of generic arguments have been provided.
|
||||
/// This is used both for datatypes and function calls.
|
||||
#[instrument(skip(tcx, gen_pos), level = "debug")]
|
||||
#[instrument(skip(cx, gen_pos), level = "debug")]
|
||||
pub(crate) fn check_generic_arg_count(
|
||||
tcx: TyCtxt<'_>,
|
||||
cx: &dyn HirTyLowerer<'_>,
|
||||
def_id: DefId,
|
||||
seg: &hir::PathSegment<'_>,
|
||||
gen_params: &ty::Generics,
|
||||
|
@ -456,11 +458,11 @@ pub(crate) fn check_generic_arg_count(
|
|||
if gen_pos != GenericArgPosition::Type
|
||||
&& let Some(c) = gen_args.constraints.first()
|
||||
{
|
||||
prohibit_assoc_item_constraint(tcx, c, None);
|
||||
prohibit_assoc_item_constraint(cx, c, None);
|
||||
}
|
||||
|
||||
let explicit_late_bound =
|
||||
prohibit_explicit_late_bound_lifetimes(tcx, gen_params, gen_args, gen_pos);
|
||||
prohibit_explicit_late_bound_lifetimes(cx, gen_params, gen_args, gen_pos);
|
||||
|
||||
let mut invalid_args = vec![];
|
||||
|
||||
|
@ -486,8 +488,8 @@ pub(crate) fn check_generic_arg_count(
|
|||
GenericArgsInfo::MissingLifetimes { num_missing_args }
|
||||
};
|
||||
|
||||
let reported = tcx.dcx().emit_err(WrongNumberOfGenericArgs::new(
|
||||
tcx,
|
||||
let reported = cx.dcx().emit_err(WrongNumberOfGenericArgs::new(
|
||||
cx.tcx(),
|
||||
gen_args_info,
|
||||
seg,
|
||||
gen_params,
|
||||
|
@ -571,9 +573,9 @@ pub(crate) fn check_generic_arg_count(
|
|||
debug!(?gen_args_info);
|
||||
|
||||
let reported = gen_args.has_err().unwrap_or_else(|| {
|
||||
tcx.dcx()
|
||||
cx.dcx()
|
||||
.create_err(WrongNumberOfGenericArgs::new(
|
||||
tcx,
|
||||
cx.tcx(),
|
||||
gen_args_info,
|
||||
seg,
|
||||
gen_params,
|
||||
|
@ -621,7 +623,7 @@ pub(crate) fn check_generic_arg_count(
|
|||
/// Prohibits explicit lifetime arguments if late-bound lifetime parameters
|
||||
/// are present. This is used both for datatypes and function calls.
|
||||
pub(crate) fn prohibit_explicit_late_bound_lifetimes(
|
||||
tcx: TyCtxt<'_>,
|
||||
cx: &dyn HirTyLowerer<'_>,
|
||||
def: &ty::Generics,
|
||||
args: &hir::GenericArgs<'_>,
|
||||
position: GenericArgPosition,
|
||||
|
@ -642,13 +644,13 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
|
|||
if position == GenericArgPosition::Value
|
||||
&& args.num_lifetime_params() != param_counts.lifetimes
|
||||
{
|
||||
struct_span_code_err!(tcx.dcx(), span, E0794, "{}", msg)
|
||||
struct_span_code_err!(cx.dcx(), span, E0794, "{}", msg)
|
||||
.with_span_note(span_late, note)
|
||||
.emit();
|
||||
} else {
|
||||
let mut multispan = MultiSpan::from_span(span);
|
||||
multispan.push_span_label(span_late, note);
|
||||
tcx.node_span_lint(
|
||||
cx.tcx().node_span_lint(
|
||||
LATE_BOUND_LIFETIME_ARGUMENTS,
|
||||
args.args[0].hir_id(),
|
||||
multispan,
|
||||
|
|
|
@ -60,7 +60,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let msg = "trait objects must include the `dyn` keyword";
|
||||
let label = "add `dyn` keyword before this trait";
|
||||
let mut diag =
|
||||
rustc_errors::struct_span_code_err!(tcx.dcx(), self_ty.span, E0782, "{}", msg);
|
||||
rustc_errors::struct_span_code_err!(self.dcx(), self_ty.span, E0782, "{}", msg);
|
||||
if self_ty.span.can_be_used_for_suggestions()
|
||||
&& !self.maybe_suggest_impl_trait(self_ty, &mut diag)
|
||||
{
|
||||
|
|
|
@ -28,7 +28,8 @@ use crate::require_c_abi_if_c_variadic;
|
|||
use rustc_ast::TraitObjectSyntax;
|
||||
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
|
||||
use rustc_errors::{
|
||||
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, FatalError,
|
||||
codes::*, struct_span_code_err, Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed,
|
||||
FatalError,
|
||||
};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
|
||||
|
@ -102,6 +103,8 @@ pub enum RegionInferReason<'a> {
|
|||
pub trait HirTyLowerer<'tcx> {
|
||||
fn tcx(&self) -> TyCtxt<'tcx>;
|
||||
|
||||
fn dcx(&self) -> DiagCtxtHandle<'_>;
|
||||
|
||||
/// Returns the [`LocalDefId`] of the overarching item whose constituents get lowered.
|
||||
fn item_def_id(&self) -> LocalDefId;
|
||||
|
||||
|
@ -323,7 +326,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
ty::BoundConstness::NotConst,
|
||||
);
|
||||
if let Some(c) = item_segment.args().constraints.first() {
|
||||
prohibit_assoc_item_constraint(self.tcx(), c, Some((def_id, item_segment, span)));
|
||||
prohibit_assoc_item_constraint(self, c, Some((def_id, item_segment, span)));
|
||||
}
|
||||
args
|
||||
}
|
||||
|
@ -394,7 +397,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
|
||||
let mut arg_count = check_generic_arg_count(
|
||||
tcx,
|
||||
self,
|
||||
def_id,
|
||||
segment,
|
||||
generics,
|
||||
|
@ -562,7 +565,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
&& generics.has_self
|
||||
&& !tcx.has_attr(def_id, sym::const_trait)
|
||||
{
|
||||
let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
|
||||
let reported = self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
|
||||
span,
|
||||
modifier: constness.as_str(),
|
||||
});
|
||||
|
@ -579,7 +582,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
incorrect_args: &arg_count.correct,
|
||||
};
|
||||
let args = lower_generic_args(
|
||||
tcx,
|
||||
self,
|
||||
def_id,
|
||||
parent_args,
|
||||
self_ty.is_some(),
|
||||
|
@ -609,7 +612,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
ty::BoundConstness::NotConst,
|
||||
);
|
||||
if let Some(c) = item_segment.args().constraints.first() {
|
||||
prohibit_assoc_item_constraint(self.tcx(), c, Some((item_def_id, item_segment, span)));
|
||||
prohibit_assoc_item_constraint(self, c, Some((item_def_id, item_segment, span)));
|
||||
}
|
||||
args
|
||||
}
|
||||
|
@ -715,7 +718,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
// would not be well-formed!
|
||||
if polarity != ty::PredicatePolarity::Positive {
|
||||
assert!(
|
||||
self.tcx().dcx().has_errors().is_some(),
|
||||
self.dcx().has_errors().is_some(),
|
||||
"negative trait bounds should not have assoc item constraints",
|
||||
);
|
||||
continue;
|
||||
|
@ -761,11 +764,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
constness,
|
||||
);
|
||||
if let Some(c) = trait_segment.args().constraints.first() {
|
||||
prohibit_assoc_item_constraint(
|
||||
self.tcx(),
|
||||
c,
|
||||
Some((trait_def_id, trait_segment, span)),
|
||||
);
|
||||
prohibit_assoc_item_constraint(self, c, Some((trait_def_id, trait_segment, span)));
|
||||
}
|
||||
ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args)
|
||||
}
|
||||
|
@ -887,7 +886,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
let assoc_kind_str = assoc_kind_str(assoc_kind);
|
||||
let ty_param_name = &ty_param_name.to_string();
|
||||
let mut err = tcx.dcx().create_err(crate::errors::AmbiguousAssocItem {
|
||||
let mut err = self.dcx().create_err(crate::errors::AmbiguousAssocItem {
|
||||
span,
|
||||
assoc_kind: assoc_kind_str,
|
||||
assoc_name,
|
||||
|
@ -1059,7 +1058,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
// trait reference.
|
||||
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
|
||||
// A cycle error occurred, most likely.
|
||||
tcx.dcx().span_bug(span, "expected cycle error");
|
||||
self.dcx().span_bug(span, "expected cycle error");
|
||||
};
|
||||
|
||||
self.probe_single_bound_for_assoc_item(
|
||||
|
@ -1089,10 +1088,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let reported = if variant_resolution.is_some() {
|
||||
// Variant in type position
|
||||
let msg = format!("expected type, found variant `{assoc_ident}`");
|
||||
tcx.dcx().span_err(span, msg)
|
||||
self.dcx().span_err(span, msg)
|
||||
} else if qself_ty.is_enum() {
|
||||
let mut err = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
assoc_ident.span,
|
||||
E0599,
|
||||
"no variant named `{}` found for enum `{}`",
|
||||
|
@ -1133,7 +1132,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
} else if let ty::Alias(ty::Opaque, alias_ty) = qself_ty.kind() {
|
||||
// `<impl Trait as OtherTrait>::Assoc` makes no sense.
|
||||
struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
tcx.def_span(alias_ty.def_id),
|
||||
E0667,
|
||||
"`impl Trait` is not allowed in path parameters"
|
||||
|
@ -1404,7 +1403,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let tcx = self.tcx();
|
||||
|
||||
if !tcx.visibility(item_def_id).is_accessible_from(scope, tcx) {
|
||||
let reported = tcx.dcx().emit_err(crate::errors::AssocItemIsPrivate {
|
||||
let reported = self.dcx().emit_err(crate::errors::AssocItemIsPrivate {
|
||||
span,
|
||||
kind: tcx.def_descr(item_def_id),
|
||||
name: ident,
|
||||
|
@ -1564,7 +1563,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
for segment in segments {
|
||||
// Only emit the first error to avoid overloading the user with error messages.
|
||||
if let Some(c) = segment.args().constraints.first() {
|
||||
return Err(prohibit_assoc_item_constraint(self.tcx(), c, None));
|
||||
return Err(prohibit_assoc_item_constraint(self, c, None));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1824,7 +1823,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
// `AlwaysApplicable` impl needs a `T: ?Sized` bound for
|
||||
// this to compile if we were to normalize here.
|
||||
if forbid_generic && ty.has_param() {
|
||||
let mut err = tcx.dcx().struct_span_err(
|
||||
let mut err = self.dcx().struct_span_err(
|
||||
path.span,
|
||||
"generic `Self` types are currently not permitted in anonymous constants",
|
||||
);
|
||||
|
@ -1894,7 +1893,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
);
|
||||
Ty::new_error(
|
||||
self.tcx(),
|
||||
self.tcx().dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
|
||||
self.dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
|
||||
)
|
||||
}
|
||||
_ => span_bug!(span, "unexpected resolution: {:?}", path.res),
|
||||
|
@ -1967,7 +1966,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let sig_span = self.tcx().def_span(sig_id);
|
||||
let mut try_emit = |descr| {
|
||||
if emit {
|
||||
self.tcx().dcx().emit_err(crate::errors::NotSupportedDelegation {
|
||||
self.dcx().emit_err(crate::errors::NotSupportedDelegation {
|
||||
span,
|
||||
descr,
|
||||
callee_span: sig_span,
|
||||
|
@ -2017,7 +2016,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
) -> Ty<'tcx> {
|
||||
if self.check_delegation_constraints(sig_id, span, idx == hir::InferDelegationKind::Output)
|
||||
{
|
||||
let e = self.tcx().dcx().span_delayed_bug(span, "not supported delegation case");
|
||||
let e = self.dcx().span_delayed_bug(span, "not supported delegation case");
|
||||
self.set_tainted_by_errors(e);
|
||||
return Ty::new_error(self.tcx(), e);
|
||||
};
|
||||
|
@ -2183,7 +2182,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
let ty = self.lower_ty(ty);
|
||||
let pat_ty = match pat.kind {
|
||||
hir::PatKind::Wild => {
|
||||
let err = tcx.dcx().emit_err(WildPatTy { span: pat.span });
|
||||
let err = self.dcx().emit_err(WildPatTy { span: pat.span });
|
||||
Ty::new_error(tcx, err)
|
||||
}
|
||||
hir::PatKind::Range(start, end, include_end) => {
|
||||
|
@ -2363,7 +2362,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
|
||||
self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
|
||||
struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
decl.output.span(),
|
||||
E0581,
|
||||
"return type references {}, which is not constrained by the fn input types",
|
||||
|
@ -2414,11 +2413,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self, generate_err))]
|
||||
fn validate_late_bound_regions(
|
||||
&self,
|
||||
fn validate_late_bound_regions<'cx>(
|
||||
&'cx self,
|
||||
constrained_regions: FxHashSet<ty::BoundRegionKind>,
|
||||
referenced_regions: FxHashSet<ty::BoundRegionKind>,
|
||||
generate_err: impl Fn(&str) -> Diag<'tcx>,
|
||||
generate_err: impl Fn(&str) -> Diag<'cx>,
|
||||
) {
|
||||
for br in referenced_regions.difference(&constrained_regions) {
|
||||
let br_name = match *br {
|
||||
|
@ -2484,7 +2483,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
// error.
|
||||
let r = derived_region_bounds[0];
|
||||
if derived_region_bounds[1..].iter().any(|r1| r != *r1) {
|
||||
self.set_tainted_by_errors(tcx.dcx().emit_err(AmbiguousLifetimeBound { span }));
|
||||
self.dcx().emit_err(AmbiguousLifetimeBound { span });
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
|
|
|
@ -236,7 +236,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
Ty::new_misc_error(tcx).into()
|
||||
} else if arg.walk().any(|arg| arg == dummy_self.into()) {
|
||||
references_self = true;
|
||||
let guar = tcx.dcx().span_delayed_bug(
|
||||
let guar = self.dcx().span_delayed_bug(
|
||||
span,
|
||||
"trait object trait bounds reference `Self`",
|
||||
);
|
||||
|
@ -263,7 +263,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
|||
if references_self {
|
||||
let def_id = i.bottom().0.def_id();
|
||||
let reported = struct_span_code_err!(
|
||||
tcx.dcx(),
|
||||
self.dcx(),
|
||||
i.bottom().1,
|
||||
E0038,
|
||||
"the {} `{}` cannot be made into an object",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue