Skip to content

Rollup of 5 pull requests #91080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Nov 20, 2021
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d64aea6
Fix `non-constant value` ICE (#90878)
Noratrieb Nov 15, 2021
483cff7
Add SourceMap::indentation_before.
m-ou-se Nov 4, 2021
453e242
Improve suggestion for unit Option/Result at the end of a block.
m-ou-se Nov 4, 2021
b331b66
Improve compatible enum variant suggestions.
m-ou-se Nov 4, 2021
4877756
Update tests.
m-ou-se Nov 4, 2021
5a25751
Add new tests for compatible variant diagnostics.
m-ou-se Nov 4, 2021
09e4a75
Use span_suggestions instead of multipart_suggestions.
m-ou-se Nov 4, 2021
b66fb64
Update test output.
m-ou-se Nov 16, 2021
7c7f58d
Fix case where ICE #90878 was still triggered by a leading newline
Noratrieb Nov 16, 2021
bf10c88
Make scrollbar in the sidebar always visible for visual consistency
GuillaumeGomez Nov 17, 2021
495d8ed
tidy-ignore-leading-newlines
Noratrieb Nov 17, 2021
96c37c8
Add a test with a leading newline for ICE #90878
Noratrieb Nov 17, 2021
f6392a1
Print output ty for opaque future ty
compiler-errors Nov 17, 2021
33ab512
Clarify error messages caused by re-exporting `pub(crate)` visibility…
ken-matsui Nov 5, 2021
7354bb3
Rollup merge of #90575 - m-ou-se:compatible-variant-improvements, r=e…
matthiaskrgr Nov 20, 2021
81f3ae8
Rollup merge of #90628 - ken-matsui:clarify-error-messages-caused-by-…
matthiaskrgr Nov 20, 2021
7993571
Rollup merge of #90930 - Nilstrieb:fix-non-const-value-ice, r=estebank
matthiaskrgr Nov 20, 2021
59c9c66
Rollup merge of #90983 - GuillaumeGomez:sidebar-scrollbar, r=jsha
matthiaskrgr Nov 20, 2021
3379721
Rollup merge of #91021 - compiler-errors:print_future_output, r=estebank
matthiaskrgr Nov 20, 2021
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
Prev Previous commit
Next Next commit
Improve compatible enum variant suggestions.
  • Loading branch information
m-ou-se committed Nov 16, 2021
commit b331b6608265f98eea8c3fa85dd67d3156c88ead
40 changes: 27 additions & 13 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let mut compatible_variants = expected_adt
let compatible_variants: Vec<String> = expected_adt
.variants
.iter()
.filter(|variant| variant.fields.len() == 1)
Expand All @@ -265,19 +265,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None
}
})
.peekable();
.collect();

if compatible_variants.peek().is_some() {
if let Ok(expr_text) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text));
let msg = "try using a variant of the expected enum";
err.span_suggestions(
expr.span,
msg,
suggestions,
Applicability::MaybeIncorrect,
);
}
if let [variant] = &compatible_variants[..] {
// Just a single matching variant.
err.multipart_suggestion(
&format!("try wrapping the expression in `{}`", variant),
vec![
(expr.span.shrink_to_lo(), format!("{}(", variant)),
(expr.span.shrink_to_hi(), ")".to_string()),
],
Applicability::MaybeIncorrect,
);
} else if compatible_variants.len() > 1 {
// More than one matching variant.
err.multipart_suggestions(
&format!(
"try wrapping the expression in a variant of `{}`",
self.tcx.def_path_str(expected_adt.did)
),
compatible_variants.into_iter().map(|variant| {
vec![
(expr.span.shrink_to_lo(), format!("{}(", variant)),
(expr.span.shrink_to_hi(), ")".to_string()),
]
}),
Applicability::MaybeIncorrect,
);
}
}
}
Expand Down