Skip to content

Commit 3e5800b

Browse files
authored
Rollup merge of rust-lang#98267 - compiler-errors:suggest-wildcard-arm, r=oli-obk
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 rust-lang#94866
2 parents eac1493 + 3d16c22 commit 3e5800b

File tree

14 files changed

+241
-277
lines changed

14 files changed

+241
-277
lines changed

compiler/rustc_middle/src/mir/spanview.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
667667
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
668668
let fn_decl_span = tcx.def_span(def_id);
669669
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
670-
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
670+
if fn_decl_span.eq_ctxt(body_span) { fn_decl_span.to(body_span) } else { body_span }
671671
} else {
672672
fn_decl_span
673673
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ fn non_exhaustive_match<'p, 'tcx>(
803803
let mut suggestion = None;
804804
let sm = cx.tcx.sess.source_map();
805805
match arms {
806-
[] if sp.ctxt() == expr_span.ctxt() => {
806+
[] if sp.eq_ctxt(expr_span) => {
807807
// Get the span for the empty match body `{}`.
808808
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
809809
(format!("\n{}", snippet), " ")
@@ -821,24 +821,36 @@ fn non_exhaustive_match<'p, 'tcx>(
821821
));
822822
}
823823
[only] => {
824-
let pre_indentation = if let (Some(snippet), true) = (
825-
sm.indentation_before(only.span),
826-
sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
827-
) {
828-
format!("\n{}", snippet)
824+
let (pre_indentation, is_multiline) = if let Some(snippet) = sm.indentation_before(only.span)
825+
&& let Ok(with_trailing) = sm.span_extend_while(only.span, |c| c.is_whitespace() || c == ',')
826+
&& sm.is_multiline(with_trailing)
827+
{
828+
(format!("\n{}", snippet), true)
829+
} else {
830+
(" ".to_string(), false)
831+
};
832+
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
833+
&& only.span.eq_ctxt(only.body.span)
834+
&& is_multiline
835+
{
836+
""
829837
} else {
830-
" ".to_string()
838+
","
831839
};
832-
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
833840
suggestion = Some((
834841
only.span.shrink_to_hi(),
835842
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
836843
));
837844
}
838-
[.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
845+
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
839846
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
840-
let comma =
841-
if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
847+
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
848+
&& last.span.eq_ctxt(last.body.span)
849+
{
850+
""
851+
} else {
852+
","
853+
};
842854
suggestion = Some((
843855
last.span.shrink_to_hi(),
844856
format!(

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
121121

122122
let source_file = source_map.lookup_source_file(body_span.lo());
123123
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
124-
fn_sig.span.ctxt() == body_span.ctxt()
124+
fn_sig.span.eq_ctxt(body_span)
125125
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
126126
}) {
127127
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl CoverageSpan {
195195
.expn_span
196196
.parent_callsite()
197197
.unwrap_or_else(|| bug!("macro must have a parent"))
198-
.ctxt() == body_span.ctxt()
198+
.eq_ctxt(body_span)
199199
{
200200
return Some(current_macro);
201201
}

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18941894
let names = rib
18951895
.bindings
18961896
.iter()
1897-
.filter(|(id, _)| id.span.ctxt() == label.span.ctxt())
1897+
.filter(|(id, _)| id.span.eq_ctxt(label.span))
18981898
.map(|(id, _)| id.name)
18991899
.collect::<Vec<Symbol>>();
19001900

compiler/rustc_span/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ impl Span {
537537
pub fn ctxt(self) -> SyntaxContext {
538538
self.data_untracked().ctxt
539539
}
540+
pub fn eq_ctxt(self, other: Span) -> bool {
541+
self.data_untracked().ctxt == other.data_untracked().ctxt
542+
}
540543
#[inline]
541544
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
542545
self.data_untracked().with_ctxt(ctxt)

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ impl Ident {
16411641

16421642
impl PartialEq for Ident {
16431643
fn eq(&self, rhs: &Self) -> bool {
1644-
self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
1644+
self.name == rhs.name && self.span.eq_ctxt(rhs.span)
16451645
}
16461646
}
16471647

0 commit comments

Comments
 (0)