Skip to content

Commit 5895102

Browse files
committed
debug Span::ctxt() call detection
1 parent f77dea8 commit 5895102

File tree

9 files changed

+29
-28
lines changed

9 files changed

+29
-28
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
650650
.sess
651651
.source_map()
652652
.is_multiline(call_expr.span.with_lo(callee_expr.span.hi()))
653-
&& call_expr.span.ctxt() == callee_expr.span.ctxt();
653+
&& call_expr.span.eq_ctxt(callee_expr.span);
654654
if call_is_multiline {
655655
err.span_suggestion(
656656
callee_expr.span.shrink_to_hi(),

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
494494
495495
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
496496
497-
lint_span_use_eq_ctxt = use `eq_ctxt()` not `ctxt() == ctxt()`
497+
lint_span_use_eq_ctxt = use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
498498
499499
lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target
500500
.label = target type is set here

compiler/rustc_lint/src/internal.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,9 @@ impl LateLintPass<'_> for BadOptAccess {
538538
}
539539
}
540540

541-
// some things i'm not sure about:
542-
// * is Warn the right level?
543-
// * the way i verify that the right method is being called (path + diag item check)
544-
545541
declare_tool_lint! {
546542
pub rustc::SPAN_USE_EQ_CTXT,
547-
Warn, // is this the right level?
543+
Allow,
548544
"Use of `==` with `Span::ctxt` rather than `Span::eq_ctxt`",
549545
report_in_external_macro: true
550546
}
@@ -555,25 +551,18 @@ impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
555551
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
556552
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq, .. }, lhs, rhs) = expr.kind {
557553
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
558-
cx.emit_spanned_lint(
559-
SPAN_USE_EQ_CTXT,
560-
expr.span,
561-
SpanUseEqCtxtDiag { msg: "fail" },
562-
);
554+
cx.emit_spanned_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
563555
}
564556
}
565557
}
566558
}
567559

568560
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
569561
match &expr.kind {
570-
ExprKind::MethodCall(path, receiver, _, _) => {
571-
path.ident.name.as_str() == "ctxt"
572-
&& cx
573-
.typeck_results()
574-
.type_dependent_def_id(receiver.hir_id)
575-
.is_some_and(|did| cx.tcx.is_diagnostic_item(sym::Span, did))
576-
}
562+
ExprKind::MethodCall(..) => cx
563+
.typeck_results()
564+
.type_dependent_def_id(expr.hir_id)
565+
.is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
577566

578567
_ => false,
579568
}

compiler/rustc_lint/src/lints.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,7 @@ pub struct QueryInstability {
902902

903903
#[derive(LintDiagnostic)]
904904
#[diag(lint_span_use_eq_ctxt)]
905-
pub struct SpanUseEqCtxtDiag<'a> {
906-
pub msg: &'a str,
907-
}
905+
pub struct SpanUseEqCtxtDiag;
908906

909907
#[derive(LintDiagnostic)]
910908
#[diag(lint_tykind_kind)]

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a> CoverageSpansGenerator<'a> {
404404

405405
let Some(visible_macro) = curr.visible_macro(self.body_span) else { return };
406406
if let Some(prev) = &self.some_prev
407-
&& prev.expn_span.ctxt() == curr.expn_span.ctxt()
407+
&& prev.expn_span.eq_ctxt(curr.expn_span)
408408
{
409409
return;
410410
}

compiler/rustc_span/src/span_encoding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ use rustc_data_structures::fx::FxIndexSet;
7575
/// the dependency to the parent definition's span. This is performed
7676
/// using the callback `SPAN_TRACK` to access the query engine.
7777
///
78-
#[cfg_attr(not(test), rustc_diagnostic_item = "Span")]
7978
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
8079
#[rustc_pass_by_value]
8180
pub struct Span {
@@ -213,6 +212,7 @@ impl Span {
213212

214213
/// This function is used as a fast path when decoding the full `SpanData` is not necessary.
215214
/// It's a cut-down version of `data_untracked`.
215+
#[cfg_attr(not(test), rustc_diagnostic_item = "SpanCtxt")]
216216
#[inline]
217217
pub fn ctxt(self) -> SyntaxContext {
218218
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ symbols! {
303303
SliceIndex,
304304
SliceIter,
305305
Some,
306-
Span,
306+
SpanCtxt,
307307
String,
308308
StructuralEq,
309309
StructuralPartialEq,
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
// Test the `rustc::span_use_eq_ctxt` internal lint
12
// compile-flags: -Z unstable-options
23

34
#![feature(rustc_private)]
45
#![deny(rustc::span_use_eq_ctxt)]
6+
#![crate_type = "lib"]
57

68
extern crate rustc_span;
79
use rustc_span::Span;
810

911
pub fn f(s: Span, t: Span) -> bool {
10-
s.ctxt() == t.ctxt() //~ ERROR use of span ctxt
12+
s.ctxt() == t.ctxt() //~ ERROR use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
1113
}
12-
13-
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
2+
--> $DIR/span_use_eq_ctxt.rs:12:5
3+
|
4+
LL | s.ctxt() == t.ctxt()
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/span_use_eq_ctxt.rs:5:9
9+
|
10+
LL | #![deny(rustc::span_use_eq_ctxt)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)