Replace some Option<Diag> with Result<(), Diag>

This commit is contained in:
Oli Scherer 2024-06-12 14:59:32 +00:00
parent 2733b8ab8d
commit a6217011f6
7 changed files with 95 additions and 77 deletions

View File

@ -2888,7 +2888,7 @@ fn report_local_value_does_not_live_long_enough(
.. ..
} = explanation } = 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,
borrow_span, borrow_span,
span, span,
@ -3075,7 +3075,7 @@ fn report_temporary_value_does_not_live_long_enough(
if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } = if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } =
explanation 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,
proper_span, proper_span,
span, span,
@ -3237,11 +3237,11 @@ fn try_report_cannot_return_reference_to_local(
return_span: Span, return_span: Span,
category: ConstraintCategory<'tcx>, category: ConstraintCategory<'tcx>,
opt_place_desc: Option<&String>, opt_place_desc: Option<&String>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
let return_kind = match category { let return_kind = match category {
ConstraintCategory::Return(_) => "return", ConstraintCategory::Return(_) => "return",
ConstraintCategory::Yield => "yield", ConstraintCategory::Yield => "yield",
_ => return None, _ => return Ok(()),
}; };
// FIXME use a better heuristic than Spans // FIXME use a better heuristic than Spans
@ -3317,7 +3317,7 @@ fn try_report_cannot_return_reference_to_local(
} }
} }
Some(err) Err(err)
} }
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self))]

View File

@ -4,7 +4,7 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; use rustc_infer::infer::DefineOpaqueTypes;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
@ -166,7 +166,7 @@ fn adjust_expr_for_assert_eq_macro(
/// Requires that the two types unify, and prints an error message if /// Requires that the two types unify, and prints an error message if
/// they don't. /// they don't.
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) { 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(); e.emit();
} }
} }
@ -176,7 +176,7 @@ pub fn demand_suptype_diag(
sp: Span, sp: Span,
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
self.demand_suptype_with_origin(&self.misc(sp), expected, actual) self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
} }
@ -186,18 +186,15 @@ pub fn demand_suptype_with_origin(
cause: &ObligationCause<'tcx>, cause: &ObligationCause<'tcx>,
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
match self.at(cause, self.param_env).sup(DefineOpaqueTypes::Yes, expected, actual) { self.at(cause, self.param_env)
Ok(InferOk { obligations, value: () }) => { .sup(DefineOpaqueTypes::Yes, expected, actual)
self.register_predicates(obligations); .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
None .map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e))
}
Err(e) => Some(self.err_ctxt().report_mismatched_types(cause, expected, actual, e)),
}
} }
pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) { 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(); err.emit();
} }
} }
@ -207,7 +204,7 @@ pub fn demand_eqtype_diag(
sp: Span, sp: Span,
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
self.demand_eqtype_with_origin(&self.misc(sp), expected, actual) self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
} }
@ -216,14 +213,11 @@ pub fn demand_eqtype_with_origin(
cause: &ObligationCause<'tcx>, cause: &ObligationCause<'tcx>,
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
match self.at(cause, self.param_env).eq(DefineOpaqueTypes::Yes, expected, actual) { self.at(cause, self.param_env)
Ok(InferOk { obligations, value: () }) => { .eq(DefineOpaqueTypes::Yes, expected, actual)
self.register_predicates(obligations); .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
None .map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e))
}
Err(e) => Some(self.err_ctxt().report_mismatched_types(cause, expected, actual, e)),
}
} }
pub fn demand_coerce( pub fn demand_coerce(
@ -234,12 +228,17 @@ pub fn demand_coerce(
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>, expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
allow_two_phase: AllowTwoPhase, allow_two_phase: AllowTwoPhase,
) -> Ty<'tcx> { ) -> Ty<'tcx> {
let (ty, err) = match self.demand_coerce_diag(expr, checked_ty, expected, expected_ty_expr, allow_two_phase)
self.demand_coerce_diag(expr, checked_ty, expected, expected_ty_expr, allow_two_phase); {
if let Some(err) = err { Ok(ty) => ty,
err.emit(); 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`. /// Checks that the type of `expr` can be coerced to `expected`.
@ -254,11 +253,11 @@ pub fn demand_coerce_diag(
expected: Ty<'tcx>, expected: Ty<'tcx>,
mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>, mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
allow_two_phase: AllowTwoPhase, allow_two_phase: AllowTwoPhase,
) -> (Ty<'tcx>, Option<Diag<'tcx>>) { ) -> Result<Ty<'tcx>, Diag<'tcx>> {
let expected = self.resolve_vars_with_obligations(expected); let expected = self.resolve_vars_with_obligations(expected);
let e = match self.coerce(expr, checked_ty, expected, allow_two_phase, None) { 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, Err(e) => e,
}; };
@ -275,7 +274,7 @@ pub fn demand_coerce_diag(
self.emit_coerce_suggestions(&mut err, expr, expr_ty, expected, expected_ty_expr, Some(e)); 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 /// Notes the point at which a variable is constrained to some type incompatible

View File

@ -87,7 +87,7 @@ pub fn check_expr_has_type_or_error(
ty = adj_ty; 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( let _ = self.emit_type_mismatch_suggestions(
&mut err, &mut err,
expr.peel_drop_temps(), expr.peel_drop_temps(),
@ -1132,7 +1132,7 @@ fn check_expr_assign(
// say that the user intended to write `lhs == rhs` instead of `lhs = rhs`. // say that the user intended to write `lhs == rhs` instead of `lhs = rhs`.
// The likely cause of this is `if foo = bar { .. }`. // The likely cause of this is `if foo = bar { .. }`.
let actual_ty = self.tcx.types.unit; 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 lhs_ty = self.check_expr(lhs);
let rhs_ty = self.check_expr(rhs); let rhs_ty = self.check_expr(rhs);
let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| { let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
@ -1236,7 +1236,7 @@ fn check_expr_assign(
// This is (basically) inlined `check_expr_coercible_to_type`, but we want // This is (basically) inlined `check_expr_coercible_to_type`, but we want
// to suggest an additional fixup here in `suggest_deref_binop`. // to suggest an additional fixup here in `suggest_deref_binop`.
let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty); 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) self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
{ {
suggest_deref_binop(&mut diag, rhs_ty); suggest_deref_binop(&mut diag, rhs_ty);
@ -1741,10 +1741,9 @@ fn check_expr_struct_fields(
// Make sure to give a type to the field even if there's // Make sure to give a type to the field even if there's
// an error, so we can continue type-checking. // an error, so we can continue type-checking.
let ty = self.check_expr_with_hint(field.expr, field_type); let ty = self.check_expr_with_hint(field.expr, field_type);
let (_, diag) = let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
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 idx == hir_fields.len() - 1 {
if remaining_fields.is_empty() { if remaining_fields.is_empty() {
self.suggest_fru_from_range_and_emit(field, variant, args, diag); self.suggest_fru_from_range_and_emit(field, variant, args, diag);

View File

@ -1578,7 +1578,7 @@ pub fn check_decl_initializer(
// type of the place it is referencing, and not some // type of the place it is referencing, and not some
// supertype thereof. // supertype thereof.
let init_ty = self.check_expr_with_needs(init, Needs::maybe_mut_place(m)); 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( self.emit_type_mismatch_suggestions(
&mut diag, &mut diag,
init.peel_drop_temps(), init.peel_drop_temps(),
@ -1624,7 +1624,7 @@ pub(in super::super) fn check_decl(&self, decl: Declaration<'tcx>) {
let previous_diverges = self.diverges.get(); let previous_diverges = self.diverges.get();
let else_ty = self.check_block_with_expected(blk, NoExpectation); let else_ty = self.check_block_with_expected(blk, NoExpectation);
let cause = self.cause(blk.span, ObligationCauseCode::LetElse); 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(); err.emit();
} }

View File

@ -105,15 +105,16 @@ fn demand_eqtype_pat_diag(
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
ti: &TopInfo<'tcx>, ti: &TopInfo<'tcx>,
) -> Option<Diag<'tcx>> { ) -> Result<(), Diag<'tcx>> {
let mut diag = self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)?; .map_err(|mut diag| {
if let Some(expr) = ti.origin_expr { if let Some(expr) = ti.origin_expr {
self.suggest_fn_call(&mut diag, expr, expected, |output| { self.suggest_fn_call(&mut diag, expr, expected, |output| {
self.can_eq(self.param_env, output, actual) self.can_eq(self.param_env, output, actual)
}); });
} }
Some(diag) diag
})
} }
fn demand_eqtype_pat( fn demand_eqtype_pat(
@ -122,10 +123,8 @@ fn demand_eqtype_pat(
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
ti: &TopInfo<'tcx>, ti: &TopInfo<'tcx>,
) { ) -> Result<(), ErrorGuaranteed> {
if let Some(err) = self.demand_eqtype_pat_diag(cause_span, expected, actual, ti) { self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map_err(|err| err.emit())
err.emit();
}
} }
} }
@ -509,7 +508,7 @@ fn check_pat_lit(
// //
// then that's equivalent to there existing a LUB. // then that's equivalent to there existing a LUB.
let cause = self.pattern_cause(ti, span); 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( err.emit_unless(
ti.span ti.span
.filter(|&s| { .filter(|&s| {
@ -562,7 +561,7 @@ fn check_pat_range(
// Subtyping doesn't matter here, as the value is some kind of scalar. // Subtyping doesn't matter here, as the value is some kind of scalar.
let demand_eqtype = |x: &mut _, y| { let demand_eqtype = |x: &mut _, y| {
if let Some((ref mut fail, x_ty, x_span)) = *x 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 { if let Some((_, y_ty, y_span)) = y {
self.endpoint_has_type(&mut err, y_span, y_ty); self.endpoint_has_type(&mut err, y_span, y_ty);
@ -736,7 +735,9 @@ fn check_pat_ident(
// Otherwise, the type of x is the expected type `T`. // 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). 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 // If there are multiple arms, make sure they all agree on
// what the type of the binding `x` ought to be. // what the type of the binding `x` ought to be.
@ -763,7 +764,7 @@ fn check_binding_alt_eq_ty(
ti: &TopInfo<'tcx>, ti: &TopInfo<'tcx>,
) { ) {
let var_ty = self.local_ty(span, var_id); 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 hir = self.tcx.hir();
let var_ty = self.resolve_vars_if_possible(var_ty); let var_ty = self.resolve_vars_if_possible(var_ty);
let msg = format!("first introduced with type `{var_ty}` here"); let msg = format!("first introduced with type `{var_ty}` here");
@ -986,7 +987,7 @@ fn check_pat_struct(
}; };
// Type-check the path. // 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. // Type-check subpatterns.
if self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) { if self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
@ -1050,7 +1051,7 @@ fn check_pat_path(
// Type-check the path. // Type-check the path.
let (pat_ty, pat_res) = let (pat_ty, pat_res) =
self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id); 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.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); self.emit_bad_pat_path(err, pat, res, pat_res, pat_ty, segments);
@ -1223,7 +1224,7 @@ fn check_pat_tuple_struct(
// Type-check the tuple struct pattern against the expected type. // 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 diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, pat_info.top_info);
let had_err = diag.map(|diag| diag.emit()); let had_err = diag.map_err(|diag| diag.emit());
// Type-check subpatterns. // Type-check subpatterns.
if subpats.len() == variant.fields.len() if subpats.len() == variant.fields.len()
@ -1244,7 +1245,7 @@ fn check_pat_tuple_struct(
None, None,
); );
} }
if let Some(e) = had_err { if let Err(e) = had_err {
on_error(e); on_error(e);
return Ty::new_error(tcx, e); return Ty::new_error(tcx, e);
} }
@ -1256,7 +1257,7 @@ fn check_pat_tuple_struct(
subpats, subpats,
&variant.fields.raw, &variant.fields.raw,
expected, expected,
had_err.is_some(), had_err.is_err(),
); );
on_error(e); on_error(e);
return Ty::new_error(tcx, e); return Ty::new_error(tcx, e);
@ -1444,8 +1445,7 @@ fn check_pat_tuple(
let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(span)); 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 element_tys = tcx.mk_type_list_from_iter(element_tys_iter);
let pat_ty = Ty::new_tup(tcx, element_tys); 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) { if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, pat_info.top_info) {
let reported = err.emit();
// Walk subpatterns with an expected type of `err` in this case to silence // Walk subpatterns with an expected type of `err` in this case to silence
// further errors being emitted when using the bindings. #50333 // further errors being emitted when using the bindings. #50333
let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported)); let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported));
@ -2064,20 +2064,20 @@ fn check_pat_box(
pat_info: PatInfo<'tcx, '_>, pat_info: PatInfo<'tcx, '_>,
) -> Ty<'tcx> { ) -> Ty<'tcx> {
let tcx = self.tcx; let tcx = self.tcx;
let (box_ty, inner_ty) = match self.check_dereferenceable(span, expected, inner) { let (box_ty, inner_ty) = self
Ok(()) => { .check_dereferenceable(span, expected, inner)
.and_then(|()| {
// Here, `demand::subtype` is good enough, but I don't // Here, `demand::subtype` is good enough, but I don't
// think any errors can be introduced by using `demand::eqtype`. // think any errors can be introduced by using `demand::eqtype`.
let inner_ty = self.next_ty_var(inner.span); let inner_ty = self.next_ty_var(inner.span);
let box_ty = Ty::new_box(tcx, inner_ty); let box_ty = Ty::new_box(tcx, inner_ty);
self.demand_eqtype_pat(span, expected, box_ty, pat_info.top_info); self.demand_eqtype_pat(span, expected, box_ty, pat_info.top_info)?;
(box_ty, inner_ty) Ok((box_ty, inner_ty))
} })
Err(guar) => { .unwrap_or_else(|guar| {
let err = Ty::new_error(tcx, guar); let err = Ty::new_error(tcx, guar);
(err, err) (err, err)
} });
};
self.check_pat(inner, inner_ty, pat_info); self.check_pat(inner, inner_ty, pat_info);
box_ty box_ty
} }
@ -2221,7 +2221,7 @@ fn check_pat_ref(
// Look for a case like `fn foo(&foo: u32)` and suggest // Look for a case like `fn foo(&foo: u32)` and suggest
// `fn foo(foo: &u32)` // `fn foo(foo: &u32)`
if let Some(mut err) = err { if let Err(mut err) = err {
self.borrow_pat_suggestion(&mut err, pat); self.borrow_pat_suggestion(&mut err, pat);
err.emit(); err.emit();
} }
@ -2326,7 +2326,7 @@ fn check_pat_slice(
self.try_resolve_slice_ty_to_array_ty(before, slice, span) self.try_resolve_slice_ty_to_array_ty(before, slice, span)
{ {
debug!(?resolved_arr_ty); debug!(?resolved_arr_ty);
self.demand_eqtype(span, expected, resolved_arr_ty); let _ = self.demand_eqtype(span, expected, resolved_arr_ty);
} }
} }

View File

@ -1,10 +1,11 @@
//@ known-bug: #124004 //! This test used to ICE #124004
#![feature(box_patterns)] #![feature(box_patterns)]
use std::ops::{ Deref }; use std::ops::{ Deref };
struct X(dyn Iterator<Item = &'a ()>); struct X(dyn Iterator<Item = &'a ()>);
//~^ ERROR: use of undeclared lifetime name `'a`
impl Deref for X { impl Deref for X {
type Target = isize; type Target = isize;

View File

@ -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`.