Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some report and emit errors take DefIds instead of BodyIds #108945

Merged
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
12 changes: 9 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

if !errors.is_empty() {
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id);
self.err_ctxt().report_fulfillment_errors(&errors, Some(self.inh.body_def_id));
}
}

Expand All @@ -594,7 +594,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !result.is_empty() {
mutate_fulfillment_errors(&mut result);
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id);
self.err_ctxt().report_fulfillment_errors(&result, Some(self.inh.body_def_id));
}
}

Expand Down Expand Up @@ -1411,7 +1411,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
let e = self.tainted_by_errors().unwrap_or_else(|| {
self.err_ctxt()
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
.emit_inference_failure_err(
Some(self.inh.body_def_id),
sp,
ty.into(),
E0282,
true,
)
.emit()
});
let err = self.tcx.ty_error(e);
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_hir_typeck/src/inherited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct Inherited<'tcx> {
pub(super) deferred_generator_interiors:
RefCell<Vec<(LocalDefId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,

pub(super) body_id: Option<hir::BodyId>,
pub(super) body_def_id: LocalDefId,

/// Whenever we introduce an adjustment from `!` into a type variable,
/// we record that type variable here. This is later used to inform
Expand Down Expand Up @@ -116,7 +116,6 @@ impl<'tcx> Inherited<'tcx> {
typeck_results: RefCell<ty::TypeckResults<'tcx>>,
) -> Self {
let tcx = infcx.tcx;
let body_id = tcx.hir().maybe_body_owned_by(def_id);

Inherited {
typeck_results,
Expand All @@ -130,7 +129,7 @@ impl<'tcx> Inherited<'tcx> {
deferred_asm_checks: RefCell::new(Vec::new()),
deferred_generator_interiors: RefCell::new(Vec::new()),
diverging_type_vars: RefCell::new(Default::default()),
body_id,
body_def_id: def_id,
infer_var_info: RefCell::new(Default::default()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
.infcx
.err_ctxt()
.emit_inference_failure_err(
Some(self.body.id()),
Some(self.tcx.hir().body_owner_def_id(self.body.id())),
self.span.to_span(self.tcx),
p.into(),
E0282,
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::def::{CtorOf, DefKind, Namespace};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
use rustc_middle::hir::nested_filter;
Expand Down Expand Up @@ -386,7 +386,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
#[instrument(level = "debug", skip(self, error_code))]
pub fn emit_inference_failure_err(
&self,
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
failure_span: Span,
arg: GenericArg<'tcx>,
error_code: TypeAnnotationNeeded,
Expand All @@ -403,8 +403,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
};

let mut local_visitor = FindInferSourceVisitor::new(&self, typeck_results, arg);
if let Some(body_id) = body_id {
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
if let Some(body_def_id) = body_def_id
&& let Some(body_id) = self.tcx.hir().maybe_body_owned_by(body_def_id)
spastorino marked this conversation as resolved.
Show resolved Hide resolved
{
let expr = self.tcx.hir().body(body_id).value;
local_visitor.visit_expr(expr);
}

Expand Down
64 changes: 32 additions & 32 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ use rustc_errors::{
};
use rustc_hir as hir;
use rustc_hir::def::Namespace;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::GenericParam;
use rustc_hir::Item;
use rustc_hir::Node;
use rustc_hir::{GenericParam, Item, Node};
use rustc_infer::infer::error_reporting::TypeErrCtxt;
use rustc_infer::infer::{InferOk, TypeTrace};
use rustc_middle::traits::select::OverflowError;
Expand Down Expand Up @@ -129,7 +127,7 @@ pub trait TypeErrCtxtExt<'tcx> {
fn report_fulfillment_errors(
&self,
errors: &[FulfillmentError<'tcx>],
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
) -> ErrorGuaranteed;

fn report_overflow_obligation<T>(
Expand Down Expand Up @@ -391,7 +389,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
fn report_fulfillment_errors(
&self,
errors: &[FulfillmentError<'tcx>],
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
) -> ErrorGuaranteed {
#[derive(Debug)]
struct ErrorDescriptor<'tcx> {
Expand Down Expand Up @@ -469,7 +467,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
for from_expansion in [false, true] {
for (error, suppressed) in iter::zip(errors, &is_suppressed) {
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
self.report_fulfillment_error(error, body_id);
self.report_fulfillment_error(error, body_def_id);
}
}
}
Expand Down Expand Up @@ -955,8 +953,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
);
}

let body_hir_id =
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
let body_def_id = obligation.cause.body_id;
// Try to report a help message
if is_fn_trait
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
Expand Down Expand Up @@ -1037,7 +1034,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if !self.report_similar_impl_candidates(
impl_candidates,
trait_ref,
body_hir_id,
body_def_id,
&mut err,
true,
) {
Expand Down Expand Up @@ -1073,7 +1070,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.report_similar_impl_candidates(
impl_candidates,
trait_ref,
body_hir_id,
body_def_id,
&mut err,
true,
);
Expand Down Expand Up @@ -1497,7 +1494,7 @@ trait InferCtxtPrivExt<'tcx> {
fn report_fulfillment_error(
&self,
error: &FulfillmentError<'tcx>,
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
);

fn report_projection_error(
Expand Down Expand Up @@ -1531,7 +1528,7 @@ trait InferCtxtPrivExt<'tcx> {
&self,
impl_candidates: Vec<ImplCandidate<'tcx>>,
trait_ref: ty::PolyTraitRef<'tcx>,
body_id: hir::HirId,
body_def_id: LocalDefId,
err: &mut Diagnostic,
other: bool,
) -> bool;
Expand Down Expand Up @@ -1564,7 +1561,7 @@ trait InferCtxtPrivExt<'tcx> {
fn maybe_report_ambiguity(
&self,
obligation: &PredicateObligation<'tcx>,
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
);

fn predicate_can_apply(
Expand Down Expand Up @@ -1650,7 +1647,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
fn report_fulfillment_error(
&self,
error: &FulfillmentError<'tcx>,
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
) {
match error.code {
FulfillmentErrorCode::CodeSelectionError(ref selection_error) => {
Expand All @@ -1664,7 +1661,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.report_projection_error(&error.obligation, e);
}
FulfillmentErrorCode::CodeAmbiguity => {
self.maybe_report_ambiguity(&error.obligation, body_id);
self.maybe_report_ambiguity(&error.obligation, body_def_id);
}
FulfillmentErrorCode::CodeSubtypeError(ref expected_found, ref err) => {
self.report_mismatched_types(
Expand Down Expand Up @@ -2029,7 +2026,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
&self,
impl_candidates: Vec<ImplCandidate<'tcx>>,
trait_ref: ty::PolyTraitRef<'tcx>,
body_id: hir::HirId,
body_def_id: LocalDefId,
err: &mut Diagnostic,
other: bool,
) -> bool {
Expand Down Expand Up @@ -2120,9 +2117,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// FIXME(compiler-errors): This could be generalized, both to
// be more granular, and probably look past other `#[fundamental]`
// types, too.
self.tcx
.visibility(def.did())
.is_accessible_from(body_id.owner.def_id, self.tcx)
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
} else {
true
}
Expand Down Expand Up @@ -2234,7 +2229,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
fn maybe_report_ambiguity(
&self,
obligation: &PredicateObligation<'tcx>,
body_id: Option<hir::BodyId>,
body_def_id: Option<LocalDefId>,
) {
// Unable to successfully determine, probably means
// insufficient type information, but could mean
Expand Down Expand Up @@ -2277,7 +2272,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
if let None = self.tainted_by_errors() {
self.emit_inference_failure_err(
body_id,
body_def_id,
span,
trait_ref.self_ty().skip_binder().into(),
ErrorCode::E0282,
Expand All @@ -2304,7 +2299,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let subst = data.trait_ref.substs.iter().find(|s| s.has_non_region_infer());

let mut err = if let Some(subst) = subst {
self.emit_inference_failure_err(body_id, span, subst, ErrorCode::E0283, true)
self.emit_inference_failure_err(
body_def_id,
span,
subst,
ErrorCode::E0283,
true,
)
} else {
struct_span_err!(
self.tcx.sess,
Expand Down Expand Up @@ -2348,12 +2349,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
predicate.to_opt_poly_trait_pred().unwrap(),
);
if impl_candidates.len() < 10 {
let hir =
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
self.report_similar_impl_candidates(
impl_candidates,
trait_ref,
body_id.map(|id| id.hir_id).unwrap_or(hir),
body_def_id.unwrap_or(obligation.cause.body_id),
&mut err,
false,
);
Expand All @@ -2375,9 +2374,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
}

if let (Some(body_id), Some(ty::subst::GenericArgKind::Type(_))) =
(body_id, subst.map(|subst| subst.unpack()))
if let (Some(body_def_id), Some(ty::subst::GenericArgKind::Type(_))) =
(body_def_id, subst.map(|subst| subst.unpack()))
{
let body_id = self.tcx.hir().body_owned_by(body_def_id);
let mut expr_finder = FindExprBySpan::new(span);
expr_finder.visit_expr(&self.tcx.hir().body(body_id).value);

Expand Down Expand Up @@ -2473,7 +2473,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
return;
}

self.emit_inference_failure_err(body_id, span, arg, ErrorCode::E0282, false)
self.emit_inference_failure_err(body_def_id, span, arg, ErrorCode::E0282, false)
}

ty::PredicateKind::Subtype(data) => {
Expand All @@ -2487,7 +2487,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let SubtypePredicate { a_is_expected: _, a, b } = data;
// both must be type variables, or the other would've been instantiated
assert!(a.is_ty_var() && b.is_ty_var());
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282, true)
self.emit_inference_failure_err(body_def_id, span, a.into(), ErrorCode::E0282, true)
}
ty::PredicateKind::Clause(ty::Clause::Projection(data)) => {
if predicate.references_error() || self.tainted_by_errors().is_some() {
Expand All @@ -2501,7 +2501,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.find(|g| g.has_non_region_infer());
if let Some(subst) = subst {
let mut err = self.emit_inference_failure_err(
body_id,
body_def_id,
span,
subst,
ErrorCode::E0284,
Expand Down Expand Up @@ -2530,7 +2530,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let subst = data.walk().find(|g| g.is_non_region_infer());
if let Some(subst) = subst {
let err = self.emit_inference_failure_err(
body_id,
body_def_id,
span,
subst,
ErrorCode::E0284,
Expand Down