Skip to content
Open
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
7 changes: 6 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ impl RibKind<'_> {
#[derive(Debug)]
pub(crate) struct Rib<'ra, R = Res> {
pub bindings: FxIndexMap<Ident, R>,
pub patterns_with_skipped_bindings: UnordMap<DefId, Vec<(Span, Result<(), ErrorGuaranteed>)>>,
pub patterns_with_skipped_bindings:
UnordMap<DefId, Vec<(Span, Option<Span>, Result<(), ErrorGuaranteed>)>>,
pub kind: RibKind<'ra>,
}

Expand Down Expand Up @@ -4309,6 +4310,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
.or_default()
.push((
pat.span,
match rest {
ast::PatFieldsRest::Rest(span) => Some(*span),
_ => None,
},
match rest {
ast::PatFieldsRest::Recovered(guar) => Err(*guar),
_ => Ok(()),
Expand Down
14 changes: 11 additions & 3 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,11 +1602,11 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
{
for field in fields {
if field.name == segment.ident.name {
if spans.iter().all(|(_, had_error)| had_error.is_err()) {
if spans.iter().all(|(_, _, had_error)| had_error.is_err()) {
// This resolution error will likely be fixed by fixing a
// syntax error in a pattern, so it is irrelevant to the user.
let multispan: MultiSpan =
spans.iter().map(|(s, _)| *s).collect::<Vec<_>>().into();
spans.iter().map(|(s, _, _)| *s).collect::<Vec<_>>().into();
err.span_note(
multispan,
"this pattern had a recovered parse error which likely lost \
Expand All @@ -1615,14 +1615,22 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
err.downgrade_to_delayed_bug();
}
let ty = self.r.tcx.item_name(*def_id);
for (span, _) in spans {
for (span, dotdot_span, _) in spans {
err.span_label(
*span,
format!(
"this pattern doesn't include `{field}`, which is \
available in `{ty}`",
),
);
if let Some(dotdot_span) = dotdot_span {
err.span_suggestion_hidden(
*dotdot_span,
format!("include `{field}` in the pattern"),
format!("{field}, .."),
Applicability::MaybeIncorrect,
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ LL | if let Website { url, .. } = website {
| ------------------- this pattern doesn't include `title`, which is available in `Website`
LL | println!("[{}]({})", title, url);
| ^^^^^ not found in this scope
|
= help: include `title` in the pattern

error[E0425]: cannot find value `a` in this scope
--> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:28:20
Expand All @@ -22,6 +24,7 @@ LL | if let Foo::Bar { .. } = x {
LL | println!("{a}");
| ^
|
= help: include `a` in the pattern
help: a local variable with a similar name exists
|
LL - println!("{a}");
Expand Down
Loading