use rustc_trait_selection::traits::ObligationCause;
use rustc_ast::util::parser::PREC_POSTFIX;
-use rustc_errors::{Applicability, DiagnosticBuilder};
+use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{is_range_literal, Node};
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn emit_coerce_suggestions(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
expr: &hir::Expr<'tcx>,
expr_ty: Ty<'tcx>,
expected: Ty<'tcx>,
sp: Span,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
- ) -> Option<DiagnosticBuilder<'tcx>> {
+ ) -> Option<DiagnosticBuilder<'tcx, ErrorReported>> {
self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
}
cause: &ObligationCause<'tcx>,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
- ) -> Option<DiagnosticBuilder<'tcx>> {
+ ) -> Option<DiagnosticBuilder<'tcx, ErrorReported>> {
match self.at(cause, self.param_env).sup(expected, actual) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
sp: Span,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
- ) -> Option<DiagnosticBuilder<'tcx>> {
+ ) -> Option<DiagnosticBuilder<'tcx, ErrorReported>> {
self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
}
cause: &ObligationCause<'tcx>,
expected: Ty<'tcx>,
actual: Ty<'tcx>,
- ) -> Option<DiagnosticBuilder<'tcx>> {
+ ) -> Option<DiagnosticBuilder<'tcx, ErrorReported>> {
match self.at(cause, self.param_env).eq(expected, actual) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
expected: Ty<'tcx>,
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
allow_two_phase: AllowTwoPhase,
- ) -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
+ ) -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx, ErrorReported>>) {
let expected = self.resolve_vars_with_obligations(expected);
let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase, None) {
fn annotate_expected_due_to_let_ty(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
expr: &hir::Expr<'_>,
error: TypeError<'_>,
) {
if !lhs.is_syntactic_place_expr() {
// We already emitted E0070 "invalid left-hand side of assignment", so we
// silence this.
- err.delay_as_bug();
+ err.downgrade_to_delayed_bug();
}
}
_ => {}
/// sole field is of the found type, suggest such variants. (Issue #42764)
fn suggest_compatible_variants(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
expr: &hir::Expr<'_>,
expected: Ty<'tcx>,
expr_ty: Ty<'tcx>,
let sole_field_ty = sole_field.ty(self.tcx, substs);
if self.can_coerce(expr_ty, sole_field_ty) {
let variant_path =
- with_no_trimmed_paths(|| self.tcx.def_path_str(variant.def_id));
+ with_no_trimmed_paths!(self.tcx.def_path_str(variant.def_id));
// FIXME #56861: DRYer prelude filtering
if let Some(path) = variant_path.strip_prefix("std::prelude::") {
if let Some((_, path)) = path.split_once("::") {
/// opt.map(|param| { takes_ref(param) });
/// ```
fn can_use_as_ref(&self, expr: &hir::Expr<'_>) -> Option<(Span, &'static str, String)> {
- let path = match expr.kind {
- hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) => path,
- _ => return None,
+ let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.kind else {
+ return None;
};
- let local_id = match path.res {
- hir::def::Res::Local(id) => id,
- _ => return None,
+ let hir::def::Res::Local(local_id) = path.res else {
+ return None;
};
let local_parent = self.tcx.hir().get_parent_node(local_id);
- let param_hir_id = match self.tcx.hir().find(local_parent) {
- Some(Node::Param(hir::Param { hir_id, .. })) => hir_id,
- _ => return None,
+ let Some(Node::Param(hir::Param { hir_id: param_hir_id, .. })) = self.tcx.hir().find(local_parent) else {
+ return None;
};
let param_parent = self.tcx.hir().get_parent_node(*param_hir_id);
- let (expr_hir_id, closure_fn_decl) = match self.tcx.hir().find(param_parent) {
- Some(Node::Expr(hir::Expr {
- hir_id,
- kind: hir::ExprKind::Closure(_, decl, ..),
- ..
- })) => (hir_id, decl),
- _ => return None,
+ let Some(Node::Expr(hir::Expr {
+ hir_id: expr_hir_id,
+ kind: hir::ExprKind::Closure(_, closure_fn_decl, ..),
+ ..
+ })) = self.tcx.hir().find(param_parent) else {
+ return None;
};
let expr_parent = self.tcx.hir().get_parent_node(*expr_hir_id);
let hir = self.tcx.hir().find(expr_parent);
let closure_params_len = closure_fn_decl.inputs.len();
- let (method_path, method_expr) = match (hir, closure_params_len) {
- (
- Some(Node::Expr(hir::Expr {
- kind: hir::ExprKind::MethodCall(segment, expr, _),
- ..
- })),
- 1,
- ) => (segment, expr),
- _ => return None,
+ let (
+ Some(Node::Expr(hir::Expr {
+ kind: hir::ExprKind::MethodCall(method_path, method_expr, _),
+ ..
+ })),
+ 1,
+ ) = (hir, closure_params_len) else {
+ return None;
};
let self_ty = self.typeck_results.borrow().node_type(method_expr[0].hir_id);
match (&expr.kind, expected.kind(), checked_ty.kind()) {
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (exp.kind(), check.kind()) {
(&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
- if let hir::ExprKind::Lit(_) = expr.kind {
- if let Ok(src) = sm.span_to_snippet(sp) {
- if replace_prefix(&src, "b\"", "\"").is_some() {
+ if let hir::ExprKind::Lit(_) = expr.kind
+ && let Ok(src) = sm.span_to_snippet(sp)
+ && replace_prefix(&src, "b\"", "\"").is_some()
+ {
let pos = sp.lo() + BytePos(1);
return Some((
sp.with_hi(pos),
));
}
}
- }
- }
(&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
- if let hir::ExprKind::Lit(_) = expr.kind {
- if let Ok(src) = sm.span_to_snippet(sp) {
- if replace_prefix(&src, "\"", "b\"").is_some() {
+ if let hir::ExprKind::Lit(_) = expr.kind
+ && let Ok(src) = sm.span_to_snippet(sp)
+ && replace_prefix(&src, "\"", "b\"").is_some()
+ {
return Some((
sp.shrink_to_lo(),
"consider adding a leading `b`",
Applicability::MachineApplicable,
true,
));
- }
- }
+
}
}
_ => {}
pub fn check_for_cast(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
expr: &hir::Expr<'_>,
checked_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
let in_const_context = self.tcx.hir().is_inside_const_context(expr.hir_id);
let suggest_fallible_into_or_lhs_from =
- |err: &mut DiagnosticBuilder<'_>, exp_to_found_is_fallible: bool| {
+ |err: &mut Diagnostic, exp_to_found_is_fallible: bool| {
// If we know the expression the expected type is derived from, we might be able
// to suggest a widening conversion rather than a narrowing one (which may
// panic). For example, given x: u8 and y: u32, if we know the span of "x",
};
let suggest_to_change_suffix_or_into =
- |err: &mut DiagnosticBuilder<'_>,
+ |err: &mut Diagnostic,
found_to_exp_is_fallible: bool,
exp_to_found_is_fallible: bool| {
let exp_is_lhs =
}
// Report the type inferred by the return statement.
- fn report_closure_inferred_return_type(
- &self,
- err: &mut DiagnosticBuilder<'_>,
- expected: Ty<'tcx>,
- ) {
+ fn report_closure_inferred_return_type(&self, err: &mut Diagnostic, expected: Ty<'tcx>) {
if let Some(sp) = self.ret_coercion_span.get() {
// If the closure has an explicit return type annotation, or if
// the closure's return type has been inferred from outside