debug Span::ctxt() call detection

This commit is contained in:
Arthur Lafrance 2023-10-16 01:05:11 -07:00
parent f77dea89e1
commit 5895102c4d
9 changed files with 29 additions and 28 deletions

View File

@ -650,7 +650,7 @@ fn report_invalid_callee(
.sess .sess
.source_map() .source_map()
.is_multiline(call_expr.span.with_lo(callee_expr.span.hi())) .is_multiline(call_expr.span.with_lo(callee_expr.span.hi()))
&& call_expr.span.ctxt() == callee_expr.span.ctxt(); && call_expr.span.eq_ctxt(callee_expr.span);
if call_is_multiline { if call_is_multiline {
err.span_suggestion( err.span_suggestion(
callee_expr.span.shrink_to_hi(), callee_expr.span.shrink_to_hi(),

View File

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

View File

@ -538,13 +538,9 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
} }
} }
// some things i'm not sure about:
// * is Warn the right level?
// * the way i verify that the right method is being called (path + diag item check)
declare_tool_lint! { declare_tool_lint! {
pub rustc::SPAN_USE_EQ_CTXT, pub rustc::SPAN_USE_EQ_CTXT,
Warn, // is this the right level? Allow,
"Use of `==` with `Span::ctxt` rather than `Span::eq_ctxt`", "Use of `==` with `Span::ctxt` rather than `Span::eq_ctxt`",
report_in_external_macro: true report_in_external_macro: true
} }
@ -555,11 +551,7 @@ impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq, .. }, lhs, rhs) = expr.kind { if let ExprKind::Binary(BinOp { node: BinOpKind::Eq, .. }, lhs, rhs) = expr.kind {
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) { if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
cx.emit_spanned_lint( cx.emit_spanned_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
SPAN_USE_EQ_CTXT,
expr.span,
SpanUseEqCtxtDiag { msg: "fail" },
);
} }
} }
} }
@ -567,13 +559,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match &expr.kind { match &expr.kind {
ExprKind::MethodCall(path, receiver, _, _) => { ExprKind::MethodCall(..) => cx
path.ident.name.as_str() == "ctxt" .typeck_results()
&& cx .type_dependent_def_id(expr.hir_id)
.typeck_results() .is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
.type_dependent_def_id(receiver.hir_id)
.is_some_and(|did| cx.tcx.is_diagnostic_item(sym::Span, did))
}
_ => false, _ => false,
} }

View File

@ -902,9 +902,7 @@ pub struct QueryInstability {
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_span_use_eq_ctxt)] #[diag(lint_span_use_eq_ctxt)]
pub struct SpanUseEqCtxtDiag<'a> { pub struct SpanUseEqCtxtDiag;
pub msg: &'a str,
}
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_tykind_kind)] #[diag(lint_tykind_kind)]

View File

@ -404,7 +404,7 @@ fn maybe_push_macro_name_span(&mut self) {
let Some(visible_macro) = curr.visible_macro(self.body_span) else { return }; let Some(visible_macro) = curr.visible_macro(self.body_span) else { return };
if let Some(prev) = &self.some_prev if let Some(prev) = &self.some_prev
&& prev.expn_span.ctxt() == curr.expn_span.ctxt() && prev.expn_span.eq_ctxt(curr.expn_span)
{ {
return; return;
} }

View File

@ -75,7 +75,6 @@
/// the dependency to the parent definition's span. This is performed /// the dependency to the parent definition's span. This is performed
/// using the callback `SPAN_TRACK` to access the query engine. /// using the callback `SPAN_TRACK` to access the query engine.
/// ///
#[cfg_attr(not(test), rustc_diagnostic_item = "Span")]
#[derive(Clone, Copy, Eq, PartialEq, Hash)] #[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[rustc_pass_by_value] #[rustc_pass_by_value]
pub struct Span { pub struct Span {
@ -213,6 +212,7 @@ pub fn is_dummy(self) -> bool {
/// This function is used as a fast path when decoding the full `SpanData` is not necessary. /// This function is used as a fast path when decoding the full `SpanData` is not necessary.
/// It's a cut-down version of `data_untracked`. /// It's a cut-down version of `data_untracked`.
#[cfg_attr(not(test), rustc_diagnostic_item = "SpanCtxt")]
#[inline] #[inline]
pub fn ctxt(self) -> SyntaxContext { pub fn ctxt(self) -> SyntaxContext {
if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER { if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {

View File

@ -303,7 +303,7 @@
SliceIndex, SliceIndex,
SliceIter, SliceIter,
Some, Some,
Span, SpanCtxt,
String, String,
StructuralEq, StructuralEq,
StructuralPartialEq, StructuralPartialEq,

View File

@ -1,13 +1,13 @@
// Test the `rustc::span_use_eq_ctxt` internal lint
// compile-flags: -Z unstable-options // compile-flags: -Z unstable-options
#![feature(rustc_private)] #![feature(rustc_private)]
#![deny(rustc::span_use_eq_ctxt)] #![deny(rustc::span_use_eq_ctxt)]
#![crate_type = "lib"]
extern crate rustc_span; extern crate rustc_span;
use rustc_span::Span; use rustc_span::Span;
pub fn f(s: Span, t: Span) -> bool { pub fn f(s: Span, t: Span) -> bool {
s.ctxt() == t.ctxt() //~ ERROR use of span ctxt s.ctxt() == t.ctxt() //~ ERROR use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
} }
fn main() {}

View File

@ -0,0 +1,14 @@
error: use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
--> $DIR/span_use_eq_ctxt.rs:12:5
|
LL | s.ctxt() == t.ctxt()
| ^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/span_use_eq_ctxt.rs:5:9
|
LL | #![deny(rustc::span_use_eq_ctxt)]
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error