mirror of https://github.com/rust-lang/rust.git
Rollup merge of #126320 - oli-obk:pat_ice, r=lcnr
Avoid ICES after reporting errors on erroneous patterns fixes #109812 fixes #125914 fixes #124004
This commit is contained in:
commit
2b3fb62b93
|
@ -2888,7 +2888,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
..
|
||||
} = explanation
|
||||
{
|
||||
if let Some(diag) = self.try_report_cannot_return_reference_to_local(
|
||||
if let Err(diag) = self.try_report_cannot_return_reference_to_local(
|
||||
borrow,
|
||||
borrow_span,
|
||||
span,
|
||||
|
@ -3075,7 +3075,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } =
|
||||
explanation
|
||||
{
|
||||
if let Some(diag) = self.try_report_cannot_return_reference_to_local(
|
||||
if let Err(diag) = self.try_report_cannot_return_reference_to_local(
|
||||
borrow,
|
||||
proper_span,
|
||||
span,
|
||||
|
@ -3237,11 +3237,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
return_span: Span,
|
||||
category: ConstraintCategory<'tcx>,
|
||||
opt_place_desc: Option<&String>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
let return_kind = match category {
|
||||
ConstraintCategory::Return(_) => "return",
|
||||
ConstraintCategory::Yield => "yield",
|
||||
_ => return None,
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
// FIXME use a better heuristic than Spans
|
||||
|
@ -3317,7 +3317,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
Some(err)
|
||||
Err(err)
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
|
|
|
@ -4,7 +4,7 @@ use rustc_errors::{Applicability, Diag};
|
|||
use rustc_hir as hir;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::intravisit::Visitor;
|
||||
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
|
||||
use rustc_infer::infer::DefineOpaqueTypes;
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::adjustment::AllowTwoPhase;
|
||||
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
||||
|
@ -166,7 +166,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
/// Requires that the two types unify, and prints an error message if
|
||||
/// they don't.
|
||||
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
|
||||
if let Some(e) = self.demand_suptype_diag(sp, expected, actual) {
|
||||
if let Err(e) = self.demand_suptype_diag(sp, expected, actual) {
|
||||
e.emit();
|
||||
}
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
sp: Span,
|
||||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
|
||||
}
|
||||
|
||||
|
@ -186,18 +186,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
cause: &ObligationCause<'tcx>,
|
||||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
match self.at(cause, self.param_env).sup(DefineOpaqueTypes::Yes, expected, actual) {
|
||||
Ok(InferOk { obligations, value: () }) => {
|
||||
self.register_predicates(obligations);
|
||||
None
|
||||
}
|
||||
Err(e) => Some(self.err_ctxt().report_mismatched_types(cause, expected, actual, e)),
|
||||
}
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
self.at(cause, self.param_env)
|
||||
.sup(DefineOpaqueTypes::Yes, expected, actual)
|
||||
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
|
||||
.map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e))
|
||||
}
|
||||
|
||||
pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
|
||||
if let Some(err) = self.demand_eqtype_diag(sp, expected, actual) {
|
||||
if let Err(err) = self.demand_eqtype_diag(sp, expected, actual) {
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +204,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
sp: Span,
|
||||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
|
||||
}
|
||||
|
||||
|
@ -216,14 +213,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
cause: &ObligationCause<'tcx>,
|
||||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
match self.at(cause, self.param_env).eq(DefineOpaqueTypes::Yes, expected, actual) {
|
||||
Ok(InferOk { obligations, value: () }) => {
|
||||
self.register_predicates(obligations);
|
||||
None
|
||||
}
|
||||
Err(e) => Some(self.err_ctxt().report_mismatched_types(cause, expected, actual, e)),
|
||||
}
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
self.at(cause, self.param_env)
|
||||
.eq(DefineOpaqueTypes::Yes, expected, actual)
|
||||
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
|
||||
.map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e))
|
||||
}
|
||||
|
||||
pub fn demand_coerce(
|
||||
|
@ -234,12 +228,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
|
||||
allow_two_phase: AllowTwoPhase,
|
||||
) -> Ty<'tcx> {
|
||||
let (ty, err) =
|
||||
self.demand_coerce_diag(expr, checked_ty, expected, expected_ty_expr, allow_two_phase);
|
||||
if let Some(err) = err {
|
||||
err.emit();
|
||||
match self.demand_coerce_diag(expr, checked_ty, expected, expected_ty_expr, allow_two_phase)
|
||||
{
|
||||
Ok(ty) => ty,
|
||||
Err(err) => {
|
||||
err.emit();
|
||||
// Return the original type instead of an error type here, otherwise the type of `x` in
|
||||
// `let x: u32 = ();` will be a type error, causing all subsequent usages of `x` to not
|
||||
// report errors, even though `x` is definitely `u32`.
|
||||
expected
|
||||
}
|
||||
}
|
||||
ty
|
||||
}
|
||||
|
||||
/// Checks that the type of `expr` can be coerced to `expected`.
|
||||
|
@ -254,11 +253,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
expected: Ty<'tcx>,
|
||||
mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
|
||||
allow_two_phase: AllowTwoPhase,
|
||||
) -> (Ty<'tcx>, Option<Diag<'tcx>>) {
|
||||
) -> Result<Ty<'tcx>, Diag<'tcx>> {
|
||||
let expected = self.resolve_vars_with_obligations(expected);
|
||||
|
||||
let e = match self.coerce(expr, checked_ty, expected, allow_two_phase, None) {
|
||||
Ok(ty) => return (ty, None),
|
||||
Ok(ty) => return Ok(ty),
|
||||
Err(e) => e,
|
||||
};
|
||||
|
||||
|
@ -275,7 +274,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
|
||||
self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr, Some(e));
|
||||
|
||||
(expected, Some(err))
|
||||
Err(err)
|
||||
}
|
||||
|
||||
/// Notes the point at which a variable is constrained to some type incompatible
|
||||
|
|
|
@ -87,7 +87,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
ty = adj_ty;
|
||||
}
|
||||
|
||||
if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
|
||||
if let Err(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
|
||||
let _ = self.emit_type_mismatch_suggestions(
|
||||
&mut err,
|
||||
expr.peel_drop_temps(),
|
||||
|
@ -1132,7 +1132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// say that the user intended to write `lhs == rhs` instead of `lhs = rhs`.
|
||||
// The likely cause of this is `if foo = bar { .. }`.
|
||||
let actual_ty = self.tcx.types.unit;
|
||||
let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap();
|
||||
let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap_err();
|
||||
let lhs_ty = self.check_expr(lhs);
|
||||
let rhs_ty = self.check_expr(rhs);
|
||||
let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
|
||||
|
@ -1236,7 +1236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// This is (basically) inlined `check_expr_coercible_to_type`, but we want
|
||||
// to suggest an additional fixup here in `suggest_deref_binop`.
|
||||
let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty);
|
||||
if let (_, Some(mut diag)) =
|
||||
if let Err(mut diag) =
|
||||
self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
|
||||
{
|
||||
suggest_deref_binop(&mut diag, rhs_ty);
|
||||
|
@ -1738,10 +1738,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// Make sure to give a type to the field even if there's
|
||||
// an error, so we can continue type-checking.
|
||||
let ty = self.check_expr_with_hint(field.expr, field_type);
|
||||
let (_, diag) =
|
||||
self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
|
||||
let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
|
||||
|
||||
if let Some(diag) = diag {
|
||||
if let Err(diag) = diag {
|
||||
if idx == hir_fields.len() - 1 {
|
||||
if remaining_fields.is_empty() {
|
||||
self.suggest_fru_from_range_and_emit(field, variant, args, diag);
|
||||
|
|
|
@ -1578,7 +1578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// type of the place it is referencing, and not some
|
||||
// supertype thereof.
|
||||
let init_ty = self.check_expr_with_needs(init, Needs::maybe_mut_place(m));
|
||||
if let Some(mut diag) = self.demand_eqtype_diag(init.span, local_ty, init_ty) {
|
||||
if let Err(mut diag) = self.demand_eqtype_diag(init.span, local_ty, init_ty) {
|
||||
self.emit_type_mismatch_suggestions(
|
||||
&mut diag,
|
||||
init.peel_drop_temps(),
|
||||
|
@ -1624,7 +1624,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let previous_diverges = self.diverges.get();
|
||||
let else_ty = self.check_block_with_expected(blk, NoExpectation);
|
||||
let cause = self.cause(blk.span, ObligationCauseCode::LetElse);
|
||||
if let Some(err) = self.demand_eqtype_with_origin(&cause, self.tcx.types.never, else_ty)
|
||||
if let Err(err) = self.demand_eqtype_with_origin(&cause, self.tcx.types.never, else_ty)
|
||||
{
|
||||
err.emit();
|
||||
}
|
||||
|
|
|
@ -105,15 +105,16 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
|
|||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
ti: &TopInfo<'tcx>,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
let mut diag =
|
||||
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)?;
|
||||
if let Some(expr) = ti.origin_expr {
|
||||
self.suggest_fn_call(&mut diag, expr, expected, |output| {
|
||||
self.can_eq(self.param_env, output, actual)
|
||||
});
|
||||
}
|
||||
Some(diag)
|
||||
) -> Result<(), Diag<'tcx>> {
|
||||
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
|
||||
.map_err(|mut diag| {
|
||||
if let Some(expr) = ti.origin_expr {
|
||||
self.suggest_fn_call(&mut diag, expr, expected, |output| {
|
||||
self.can_eq(self.param_env, output, actual)
|
||||
});
|
||||
}
|
||||
diag
|
||||
})
|
||||
}
|
||||
|
||||
fn demand_eqtype_pat(
|
||||
|
@ -122,10 +123,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
|
|||
expected: Ty<'tcx>,
|
||||
actual: Ty<'tcx>,
|
||||
ti: &TopInfo<'tcx>,
|
||||
) {
|
||||
if let Some(err) = self.demand_eqtype_pat_diag(cause_span, expected, actual, ti) {
|
||||
err.emit();
|
||||
}
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map_err(|err| err.emit())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -509,7 +508,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
//
|
||||
// then that's equivalent to there existing a LUB.
|
||||
let cause = self.pattern_cause(ti, span);
|
||||
if let Some(err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) {
|
||||
if let Err(err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) {
|
||||
err.emit_unless(
|
||||
ti.span
|
||||
.filter(|&s| {
|
||||
|
@ -562,7 +561,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// Subtyping doesn't matter here, as the value is some kind of scalar.
|
||||
let demand_eqtype = |x: &mut _, y| {
|
||||
if let Some((ref mut fail, x_ty, x_span)) = *x
|
||||
&& let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
|
||||
&& let Err(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
|
||||
{
|
||||
if let Some((_, y_ty, y_span)) = y {
|
||||
self.endpoint_has_type(&mut err, y_span, y_ty);
|
||||
|
@ -736,7 +735,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// Otherwise, the type of x is the expected type `T`.
|
||||
ByRef::No => expected, // As above, `T <: typeof(x)` is required, but we use equality, see (note_1).
|
||||
};
|
||||
self.demand_eqtype_pat(pat.span, eq_ty, local_ty, ti);
|
||||
|
||||
// We have a concrete type for the local, so we do not need to taint it and hide follow up errors *using* the local.
|
||||
let _ = self.demand_eqtype_pat(pat.span, eq_ty, local_ty, ti);
|
||||
|
||||
// If there are multiple arms, make sure they all agree on
|
||||
// what the type of the binding `x` ought to be.
|
||||
|
@ -763,7 +764,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
ti: &TopInfo<'tcx>,
|
||||
) {
|
||||
let var_ty = self.local_ty(span, var_id);
|
||||
if let Some(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
|
||||
if let Err(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
|
||||
let hir = self.tcx.hir();
|
||||
let var_ty = self.resolve_vars_if_possible(var_ty);
|
||||
let msg = format!("first introduced with type `{var_ty}` here");
|
||||
|
@ -986,13 +987,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
};
|
||||
|
||||
// Type-check the path.
|
||||
self.demand_eqtype_pat(pat.span, expected, pat_ty, pat_info.top_info);
|
||||
let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, pat_info.top_info);
|
||||
|
||||
// Type-check subpatterns.
|
||||
if self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
|
||||
pat_ty
|
||||
} else {
|
||||
Ty::new_misc_error(self.tcx)
|
||||
match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
|
||||
Ok(()) => pat_ty,
|
||||
Err(guar) => Ty::new_error(self.tcx, guar),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1050,7 +1050,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// Type-check the path.
|
||||
let (pat_ty, pat_res) =
|
||||
self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id);
|
||||
if let Some(err) =
|
||||
if let Err(err) =
|
||||
self.demand_suptype_with_origin(&self.pattern_cause(ti, pat.span), expected, pat_ty)
|
||||
{
|
||||
self.emit_bad_pat_path(err, pat, res, pat_res, pat_ty, segments);
|
||||
|
@ -1223,12 +1223,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
|
||||
// Type-check the tuple struct pattern against the expected type.
|
||||
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, pat_info.top_info);
|
||||
let had_err = if let Some(err) = diag {
|
||||
err.emit();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let had_err = diag.map_err(|diag| diag.emit());
|
||||
|
||||
// Type-check subpatterns.
|
||||
if subpats.len() == variant.fields.len()
|
||||
|
@ -1249,6 +1244,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
None,
|
||||
);
|
||||
}
|
||||
if let Err(e) = had_err {
|
||||
on_error(e);
|
||||
return Ty::new_error(tcx, e);
|
||||
}
|
||||
} else {
|
||||
let e = self.emit_err_pat_wrong_number_of_fields(
|
||||
pat.span,
|
||||
|
@ -1273,7 +1272,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
subpats: &'tcx [Pat<'tcx>],
|
||||
fields: &'tcx [ty::FieldDef],
|
||||
expected: Ty<'tcx>,
|
||||
had_err: bool,
|
||||
had_err: Result<(), ErrorGuaranteed>,
|
||||
) -> ErrorGuaranteed {
|
||||
let subpats_ending = pluralize!(subpats.len());
|
||||
let fields_ending = pluralize!(fields.len());
|
||||
|
@ -1330,7 +1329,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
// #67037: only do this if we could successfully type-check the expected type against
|
||||
// the tuple struct pattern. Otherwise the args could get out of range on e.g.,
|
||||
// `let P() = U;` where `P != U` with `struct P<T>(T);`.
|
||||
(ty::Adt(_, args), [field], false) => {
|
||||
(ty::Adt(_, args), [field], Ok(())) => {
|
||||
let field_ty = self.field_ty(pat_span, field, args);
|
||||
match field_ty.kind() {
|
||||
ty::Tuple(fields) => fields.len() == subpats.len(),
|
||||
|
@ -1445,8 +1444,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(span));
|
||||
let element_tys = tcx.mk_type_list_from_iter(element_tys_iter);
|
||||
let pat_ty = Ty::new_tup(tcx, element_tys);
|
||||
if let Some(err) = self.demand_eqtype_pat_diag(span, expected, pat_ty, pat_info.top_info) {
|
||||
let reported = err.emit();
|
||||
if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, pat_info.top_info) {
|
||||
// Walk subpatterns with an expected type of `err` in this case to silence
|
||||
// further errors being emitted when using the bindings. #50333
|
||||
let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported));
|
||||
|
@ -1470,7 +1468,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
fields: &'tcx [hir::PatField<'tcx>],
|
||||
has_rest_pat: bool,
|
||||
pat_info: PatInfo<'tcx, '_>,
|
||||
) -> bool {
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
let tcx = self.tcx;
|
||||
|
||||
let ty::Adt(adt, args) = adt_ty.kind() else {
|
||||
|
@ -1486,7 +1484,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
|
||||
// Keep track of which fields have already appeared in the pattern.
|
||||
let mut used_fields = FxHashMap::default();
|
||||
let mut no_field_errors = true;
|
||||
let mut result = Ok(());
|
||||
|
||||
let mut inexistent_fields = vec![];
|
||||
// Typecheck each field.
|
||||
|
@ -1495,8 +1493,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let ident = tcx.adjust_ident(field.ident, variant.def_id);
|
||||
let field_ty = match used_fields.entry(ident) {
|
||||
Occupied(occupied) => {
|
||||
no_field_errors = false;
|
||||
let guar = self.error_field_already_bound(span, field.ident, *occupied.get());
|
||||
result = Err(guar);
|
||||
Ty::new_error(tcx, guar)
|
||||
}
|
||||
Vacant(vacant) => {
|
||||
|
@ -1511,7 +1509,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
})
|
||||
.unwrap_or_else(|| {
|
||||
inexistent_fields.push(field);
|
||||
no_field_errors = false;
|
||||
Ty::new_misc_error(tcx)
|
||||
})
|
||||
}
|
||||
|
@ -1585,37 +1582,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
}
|
||||
match (inexistent_fields_err, unmentioned_err) {
|
||||
(Some(i), Some(u)) => {
|
||||
if let Some(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
|
||||
if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
|
||||
// We don't want to show the nonexistent fields error when this was
|
||||
// `Foo { a, b }` when it should have been `Foo(a, b)`.
|
||||
i.delay_as_bug();
|
||||
u.delay_as_bug();
|
||||
e.emit();
|
||||
Err(e)
|
||||
} else {
|
||||
i.emit();
|
||||
u.emit();
|
||||
Err(u.emit())
|
||||
}
|
||||
}
|
||||
(None, Some(u)) => {
|
||||
if let Some(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
|
||||
if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
|
||||
u.delay_as_bug();
|
||||
e.emit();
|
||||
Err(e)
|
||||
} else {
|
||||
u.emit();
|
||||
Err(u.emit())
|
||||
}
|
||||
}
|
||||
(Some(err), None) => {
|
||||
err.emit();
|
||||
(Some(err), None) => Err(err.emit()),
|
||||
(None, None) => {
|
||||
self.error_tuple_variant_index_shorthand(variant, pat, fields)?;
|
||||
result
|
||||
}
|
||||
(None, None)
|
||||
if let Some(err) =
|
||||
self.error_tuple_variant_index_shorthand(variant, pat, fields) =>
|
||||
{
|
||||
err.emit();
|
||||
}
|
||||
(None, None) => {}
|
||||
}
|
||||
no_field_errors
|
||||
}
|
||||
|
||||
fn error_tuple_variant_index_shorthand(
|
||||
|
@ -1623,7 +1614,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
variant: &VariantDef,
|
||||
pat: &'_ Pat<'_>,
|
||||
fields: &[hir::PatField<'_>],
|
||||
) -> Option<Diag<'_>> {
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
// if this is a tuple struct, then all field names will be numbers
|
||||
// so if any fields in a struct pattern use shorthand syntax, they will
|
||||
// be invalid identifiers (for example, Foo { 0, 1 }).
|
||||
|
@ -1645,10 +1636,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
format!("({})", self.get_suggested_tuple_struct_pattern(fields, variant)),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
return Some(err);
|
||||
return Err(err.emit());
|
||||
}
|
||||
}
|
||||
None
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn error_foreign_non_exhaustive_spat(&self, pat: &Pat<'_>, descr: &str, no_fields: bool) {
|
||||
|
@ -1804,14 +1795,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
pat: &Pat<'_>,
|
||||
fields: &'tcx [hir::PatField<'tcx>],
|
||||
variant: &ty::VariantDef,
|
||||
) -> Option<Diag<'tcx>> {
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
if let (Some(CtorKind::Fn), PatKind::Struct(qpath, pattern_fields, ..)) =
|
||||
(variant.ctor_kind(), &pat.kind)
|
||||
{
|
||||
let is_tuple_struct_match = !pattern_fields.is_empty()
|
||||
&& pattern_fields.iter().map(|field| field.ident.name.as_str()).all(is_number);
|
||||
if is_tuple_struct_match {
|
||||
return None;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
|
||||
|
@ -1839,9 +1830,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
format!("({sugg})"),
|
||||
appl,
|
||||
);
|
||||
return Some(err);
|
||||
return Err(err.emit());
|
||||
}
|
||||
None
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_suggested_tuple_struct_pattern(
|
||||
|
@ -2065,20 +2056,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
pat_info: PatInfo<'tcx, '_>,
|
||||
) -> Ty<'tcx> {
|
||||
let tcx = self.tcx;
|
||||
let (box_ty, inner_ty) = match self.check_dereferenceable(span, expected, inner) {
|
||||
Ok(()) => {
|
||||
let (box_ty, inner_ty) = self
|
||||
.check_dereferenceable(span, expected, inner)
|
||||
.and_then(|()| {
|
||||
// Here, `demand::subtype` is good enough, but I don't
|
||||
// think any errors can be introduced by using `demand::eqtype`.
|
||||
let inner_ty = self.next_ty_var(inner.span);
|
||||
let box_ty = Ty::new_box(tcx, inner_ty);
|
||||
self.demand_eqtype_pat(span, expected, box_ty, pat_info.top_info);
|
||||
(box_ty, inner_ty)
|
||||
}
|
||||
Err(guar) => {
|
||||
self.demand_eqtype_pat(span, expected, box_ty, pat_info.top_info)?;
|
||||
Ok((box_ty, inner_ty))
|
||||
})
|
||||
.unwrap_or_else(|guar| {
|
||||
let err = Ty::new_error(tcx, guar);
|
||||
(err, err)
|
||||
}
|
||||
};
|
||||
});
|
||||
self.check_pat(inner, inner_ty, pat_info);
|
||||
box_ty
|
||||
}
|
||||
|
@ -2222,7 +2213,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
|
||||
// Look for a case like `fn foo(&foo: u32)` and suggest
|
||||
// `fn foo(foo: &u32)`
|
||||
if let Some(mut err) = err {
|
||||
if let Err(mut err) = err {
|
||||
self.borrow_pat_suggestion(&mut err, pat);
|
||||
err.emit();
|
||||
}
|
||||
|
@ -2327,7 +2318,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
self.try_resolve_slice_ty_to_array_ty(before, slice, span)
|
||||
{
|
||||
debug!(?resolved_arr_ty);
|
||||
self.demand_eqtype(span, expected, resolved_arr_ty);
|
||||
let _ = self.demand_eqtype(span, expected, resolved_arr_ty);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
//@ known-bug: #109812
|
||||
|
||||
#![warn(rust_2021_incompatible_closure_captures)]
|
||||
|
||||
enum Either {
|
||||
One(X),
|
||||
Two(X),
|
||||
}
|
||||
|
||||
struct X(Y);
|
||||
|
||||
struct Y;
|
||||
|
||||
fn move_into_fnmut() {
|
||||
let x = X(Y);
|
||||
|
||||
consume_fnmut(|| {
|
||||
let Either::Two(ref mut _t) = x;
|
||||
|
||||
let X(mut _t) = x;
|
||||
});
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
//@ known-bug: rust-lang/rust#125914
|
||||
enum AstKind<'ast> {
|
||||
ExprInt,
|
||||
}
|
||||
|
||||
enum Foo {
|
||||
Bar(isize),
|
||||
Baz,
|
||||
}
|
||||
|
||||
enum Other {
|
||||
Other1(Foo),
|
||||
Other2(AstKind),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match Other::Other1(Foo::Baz) {
|
||||
::Other::Other2(::Foo::Bar(..)) => {}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
//@ known-bug: #124004
|
||||
//! This test used to ICE #124004
|
||||
|
||||
#![feature(box_patterns)]
|
||||
|
||||
use std::ops::{ Deref };
|
||||
|
||||
struct X(dyn Iterator<Item = &'a ()>);
|
||||
//~^ ERROR: use of undeclared lifetime name `'a`
|
||||
|
||||
impl Deref for X {
|
||||
type Target = isize;
|
|
@ -0,0 +1,19 @@
|
|||
error[E0261]: use of undeclared lifetime name `'a`
|
||||
--> $DIR/box-pattern-type-mismatch.rs:7:31
|
||||
|
|
||||
LL | struct X(dyn Iterator<Item = &'a ()>);
|
||||
| ^^ undeclared lifetime
|
||||
|
|
||||
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
|
||||
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
||||
|
|
||||
LL | struct X(dyn for<'a> Iterator<Item = &'a ()>);
|
||||
| +++++++
|
||||
help: consider introducing lifetime `'a` here
|
||||
|
|
||||
LL | struct X<'a>(dyn Iterator<Item = &'a ()>);
|
||||
| ++++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0261`.
|
|
@ -0,0 +1,25 @@
|
|||
//! This test used to ICE: rust-lang/rust#125914
|
||||
//! Instead of actually analyzing the erroneous patterns,
|
||||
//! we instead stop after typeck where errors are already
|
||||
//! reported.
|
||||
|
||||
enum AstKind<'ast> {
|
||||
//~^ ERROR: `'ast` is never used
|
||||
ExprInt,
|
||||
}
|
||||
|
||||
enum Foo {
|
||||
Bar(isize),
|
||||
Baz,
|
||||
}
|
||||
|
||||
enum Other {
|
||||
Other1(Foo),
|
||||
Other2(AstKind), //~ ERROR: missing lifetime specifier
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match Other::Other1(Foo::Baz) {
|
||||
::Other::Other2(::Foo::Bar(..)) => {}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
error[E0106]: missing lifetime specifier
|
||||
--> $DIR/missing_lifetime.rs:18:12
|
||||
|
|
||||
LL | Other2(AstKind),
|
||||
| ^^^^^^^ expected named lifetime parameter
|
||||
|
|
||||
help: consider introducing a named lifetime parameter
|
||||
|
|
||||
LL ~ enum Other<'a> {
|
||||
LL | Other1(Foo),
|
||||
LL ~ Other2(AstKind<'a>),
|
||||
|
|
||||
|
||||
error[E0392]: lifetime parameter `'ast` is never used
|
||||
--> $DIR/missing_lifetime.rs:6:14
|
||||
|
|
||||
LL | enum AstKind<'ast> {
|
||||
| ^^^^ unused lifetime parameter
|
||||
|
|
||||
= help: consider removing `'ast`, referring to it in a field, or using a marker such as `PhantomData`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0106, E0392.
|
||||
For more information about an error, try `rustc --explain E0106`.
|
|
@ -0,0 +1,30 @@
|
|||
//! This test used to ICE: rust-lang/rust#109812
|
||||
//! Instead of actually analyzing the erroneous patterns,
|
||||
//! we instead stop after typeck where errors are already
|
||||
//! reported.
|
||||
|
||||
#![warn(rust_2021_incompatible_closure_captures)]
|
||||
|
||||
enum Either {
|
||||
One(X),
|
||||
Two(X),
|
||||
}
|
||||
|
||||
struct X(Y);
|
||||
|
||||
struct Y;
|
||||
|
||||
fn consume_fnmut(_: impl FnMut()) {}
|
||||
|
||||
fn move_into_fnmut() {
|
||||
let x = X(Y);
|
||||
|
||||
consume_fnmut(|| {
|
||||
let Either::Two(ref mut _t) = x;
|
||||
//~^ ERROR: mismatched types
|
||||
|
||||
let X(mut _t) = x;
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {}
|
|
@ -0,0 +1,11 @@
|
|||
error[E0308]: mismatched types
|
||||
--> $DIR/type_mismatch.rs:23:13
|
||||
|
|
||||
LL | let Either::Two(ref mut _t) = x;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ - this expression has type `X`
|
||||
| |
|
||||
| expected `X`, found `Either`
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Reference in New Issue