Expand description
Error Reporting Code for the inference engine
Because of the way inference, and in particular region inference, works, it often happens that errors are not detected until far after the relevant line of code has been type-checked. Therefore, there is an elaborate system to track why a particular constraint in the inference graph arose so that we can explain to the user what gave rise to a particular error.
The system is based around a set of “origin” types. An “origin” is the
reason that a constraint or inference variable arose. There are
different “origin” enums for different kinds of constraints/variables
(e.g., TypeOrigin, RegionVariableOrigin). An origin always has
a span, but also more information so that we can generate a meaningful
error message.
Having a catalog of all the different reasons an error can arise is also useful for other reasons, like cross-referencing FAQs etc, though we are not really taking advantage of this yet.
§Region Inference
Region inference is particularly tricky because it always succeeds “in the moment” and simply registers a constraint. Then, at the end, we can compute the full graph and report errors, so we need to be able to store and later report what gave rise to the conflicting constraints.
§Subtype Trace
Determining whether T1 <: T2 often involves a number of subtypes and
subconstraints along the way. A “TypeTrace” is an extended version
of an origin that traces the types and other values that were being
compared. It is not necessarily comprehensive (in fact, at the time of
this writing it only tracks the root values being compared) but I’d
like to extend it to include significant “waypoints”. For example, if
you are comparing (T1, T2) <: (T3, T4), and the problem is that T2 <: T4 fails, I’d like the trace to include enough information to say
“in the 2nd element of the tuple”. Similarly, failures when comparing
arguments or return types in fn types should be able to cite the
specific position, etc.
§Reality vs plan
Of course, there is still a LOT of code in typeck that has yet to be ported to this system, and which relies on string concatenation at the time of error detection.
Re-exports§
use std::borrow::Cow;🔒use std::ops::ControlFlow;🔒use std::path::PathBuf;🔒use std::cmp;🔒use std::fmt;🔒use std::iter;🔒use rustc_abi::ExternAbi;🔒use rustc_attr_ir::diagnostic::CustomDiagnostic;🔒use rustc_attr_ir::diagnostic::Directive;🔒use rustc_attr_ir::diagnostic::FormatArgs;🔒use rustc_attr_ir::find_attr;🔒use rustc_data_structures::fx::FxHashSet;🔒use rustc_data_structures::fx::FxIndexMap;🔒use rustc_data_structures::fx::FxIndexSet;🔒use rustc_errors::Applicability;🔒use rustc_errors::Diag;🔒use rustc_errors::DiagStyledString;🔒use rustc_errors::IntoDiagArg;🔒use rustc_errors::MultiSpan;🔒use rustc_errors::StringPart;🔒use rustc_errors::pluralize;🔒use rustc_hir as hir;🔒use rustc_hir::def_id::CRATE_DEF_ID;🔒use rustc_hir::def_id::DefId;🔒use rustc_hir::intravisit::Visitor;🔒use rustc_infer::infer::DefineOpaqueTypes;🔒use rustc_macros::extension;🔒use rustc_middle::traits::PatternOriginExpr;🔒use rustc_middle::ty::error::ExpectedFound;🔒use rustc_middle::ty::error::TypeError;🔒use rustc_middle::ty::error::TypeErrorToStringExt;🔒use rustc_middle::ty::print::PrintTraitRefExt as _;🔒use rustc_middle::ty::print::WrapBinderMode;🔒use rustc_middle::ty::print::with_forced_trimmed_paths;🔒use rustc_middle::ty::print::with_forced_trimmed_paths;🔒use rustc_middle::ty;🔒use rustc_middle::ty::IsRigid;🔒use rustc_middle::ty::List;🔒use rustc_middle::ty::Mutability;🔒use rustc_middle::ty::ParamEnv;🔒use rustc_middle::ty::Region;🔒use rustc_middle::ty::Ty;🔒use rustc_middle::ty::TyCtxt;🔒use rustc_middle::ty::TypeFoldable;🔒use rustc_middle::ty::TypeSuperVisitable;🔒use rustc_middle::ty::TypeVisitable;🔒use rustc_middle::ty::TypeVisitableExt;🔒use rustc_middle::ty::Unnormalized;🔒use rustc_span::BytePos;🔒use rustc_span::DUMMY_SP;🔒use rustc_span::DesugaringKind;🔒use rustc_span::Pos;🔒use rustc_span::Span;🔒use rustc_span::bug;🔒use rustc_span::sym;🔒use thin_vec::ThinVec;🔒use tracing::debug;🔒use tracing::instrument;🔒use tracing::instrument;🔒use crate::diagnostics::ObligationCauseFailureCode;🔒use crate::diagnostics::TypeErrorAdditionalDiags;🔒use crate::error_reporting::TypeErrCtxt;🔒use crate::error_reporting::traits::ambiguity::CandidateSource;🔒use crate::error_reporting::traits::ambiguity::compute_applicable_impls_for_diagnostics;🔒use crate::infer;🔒use crate::infer::relate;🔒use crate::infer::relate::RelateResult;🔒use crate::infer::relate::TypeRelation;🔒use crate::infer::InferCtxt;🔒use crate::infer::InferCtxtExt as _;🔒use crate::infer::TypeTrace;🔒use crate::infer::ValuePairs;🔒use crate::traits::MatchExpressionArmCause;🔒use crate::traits::Obligation;🔒use crate::traits::ObligationCause;🔒use crate::traits::ObligationCauseCode;🔒use crate::traits::ObligationCtxt;🔒use crate::traits::specialization_graph;🔒
Modules§
Structs§
- Obligation
Cause AsDiag Arg - Newtype to allow implementing IntoDiagArg
- Same
Type 🔒Modulo Infer
Enums§
- Failure
Code - TyCategory
- This is a bare signal of what kind of type we’re dealing with.
ty::TyKindtracks extra information about each type, but we only care about the category.
Traits§
Functions§
- escape_
literal 🔒 - Makes a valid string literal from a string by escaping special characters (“ and ), unless they are already escaped.