Skip to content

Commit

Permalink
Rollup merge of #98267 - compiler-errors:suggest-wildcard-arm, r=oli-obk
Browse files Browse the repository at this point in the history
Don't omit comma when suggesting wildcard arm after macro expr

* Also adds `Span::eq_ctxt` to consolidate the various usages of `span.ctxt() == other.ctxt()`
* Also fixes an unhygenic usage of spans which caused the suggestion to render weirdly when we had one arm match in a macro
* Also always suggests a comma (i.e. even after a block) if we're rendering a wildcard arm in a single-line match (looks prettier 🌹)

Fixes #94866
  • Loading branch information
matthiaskrgr committed Jun 20, 2022
2 parents eac1493 + 3d16c22 commit 3e5800b
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 277 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/spanview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
let fn_decl_span = tcx.def_span(def_id);
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
if fn_decl_span.eq_ctxt(body_span) { fn_decl_span.to(body_span) } else { body_span }
} else {
fn_decl_span
}
Expand Down
34 changes: 23 additions & 11 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ fn non_exhaustive_match<'p, 'tcx>(
let mut suggestion = None;
let sm = cx.tcx.sess.source_map();
match arms {
[] if sp.ctxt() == expr_span.ctxt() => {
[] if sp.eq_ctxt(expr_span) => {
// Get the span for the empty match body `{}`.
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
(format!("\n{}", snippet), " ")
Expand All @@ -821,24 +821,36 @@ fn non_exhaustive_match<'p, 'tcx>(
));
}
[only] => {
let pre_indentation = if let (Some(snippet), true) = (
sm.indentation_before(only.span),
sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
) {
format!("\n{}", snippet)
let (pre_indentation, is_multiline) = if let Some(snippet) = sm.indentation_before(only.span)
&& let Ok(with_trailing) = sm.span_extend_while(only.span, |c| c.is_whitespace() || c == ',')
&& sm.is_multiline(with_trailing)
{
(format!("\n{}", snippet), true)
} else {
(" ".to_string(), false)
};
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
&& only.span.eq_ctxt(only.body.span)
&& is_multiline
{
""
} else {
" ".to_string()
","
};
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
suggestion = Some((
only.span.shrink_to_hi(),
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
));
}
[.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
let comma =
if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
&& last.span.eq_ctxt(last.body.span)
{
""
} else {
","
};
suggestion = Some((
last.span.shrink_to_hi(),
format!(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {

let source_file = source_map.lookup_source_file(body_span.lo());
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
fn_sig.span.ctxt() == body_span.ctxt()
fn_sig.span.eq_ctxt(body_span)
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
}) {
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl CoverageSpan {
.expn_span
.parent_callsite()
.unwrap_or_else(|| bug!("macro must have a parent"))
.ctxt() == body_span.ctxt()
.eq_ctxt(body_span)
{
return Some(current_macro);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let names = rib
.bindings
.iter()
.filter(|(id, _)| id.span.ctxt() == label.span.ctxt())
.filter(|(id, _)| id.span.eq_ctxt(label.span))
.map(|(id, _)| id.name)
.collect::<Vec<Symbol>>();

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ impl Span {
pub fn ctxt(self) -> SyntaxContext {
self.data_untracked().ctxt
}
pub fn eq_ctxt(self, other: Span) -> bool {
self.data_untracked().ctxt == other.data_untracked().ctxt
}
#[inline]
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
self.data_untracked().with_ctxt(ctxt)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ impl Ident {

impl PartialEq for Ident {
fn eq(&self, rhs: &Self) -> bool {
self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
self.name == rhs.name && self.span.eq_ctxt(rhs.span)
}
}

Expand Down
Loading

0 comments on commit 3e5800b

Please sign in to comment.