mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
Auto merge of #10980 - Alexendoo:format-args-incremental-spans, r=dswij
Fix `find_format_arg_expr` when incremental compilation is enabled Fixes #10969 [`lower_span`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/struct.LoweringContext.html#method.lower_span) gives AST spans a [parent](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.SpanData.html#structfield.parent) when lowering to the HIR. That meant the `==` span comparison would return false even though the spans are pointing to the same thing. We now ignore the parent when comparing the spans Debugging this was quite the puzzle, because the parent is not included in the debug output of `Span`s or `SpanData` 😬 changelog: none
This commit is contained in:
commit
ebcbba93ad
@ -9,7 +9,7 @@ use rustc_hir::{self as hir, Expr, ExprKind, HirId, Node, QPath};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_span::def_id::DefId;
|
||||
use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
|
||||
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, Symbol};
|
||||
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, SpanData, Symbol};
|
||||
use std::cell::RefCell;
|
||||
use std::ops::ControlFlow;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@ -415,8 +415,18 @@ pub fn find_format_arg_expr<'hir, 'ast>(
|
||||
start: &'hir Expr<'hir>,
|
||||
target: &'ast FormatArgument,
|
||||
) -> Result<&'hir rustc_hir::Expr<'hir>, &'ast rustc_ast::Expr> {
|
||||
let SpanData {
|
||||
lo,
|
||||
hi,
|
||||
ctxt,
|
||||
parent: _,
|
||||
} = target.expr.span.data();
|
||||
|
||||
for_each_expr(start, |expr| {
|
||||
if expr.span == target.expr.span {
|
||||
// When incremental compilation is enabled spans gain a parent during AST to HIR lowering,
|
||||
// since we're comparing an AST span to a HIR one we need to ignore the parent field
|
||||
let data = expr.span.data();
|
||||
if data.lo == lo && data.hi == hi && data.ctxt == ctxt {
|
||||
ControlFlow::Break(expr)
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
|
9
tests/ui/to_string_in_format_args_incremental.fixed
Normal file
9
tests/ui/to_string_in_format_args_incremental.fixed
Normal file
@ -0,0 +1,9 @@
|
||||
//@run-rustfix
|
||||
//@compile-flags: -C incremental=target/debug/test/incr
|
||||
|
||||
// see https://github.com/rust-lang/rust-clippy/issues/10969
|
||||
|
||||
fn main() {
|
||||
let s = "Hello, world!";
|
||||
println!("{}", s);
|
||||
}
|
9
tests/ui/to_string_in_format_args_incremental.rs
Normal file
9
tests/ui/to_string_in_format_args_incremental.rs
Normal file
@ -0,0 +1,9 @@
|
||||
//@run-rustfix
|
||||
//@compile-flags: -C incremental=target/debug/test/incr
|
||||
|
||||
// see https://github.com/rust-lang/rust-clippy/issues/10969
|
||||
|
||||
fn main() {
|
||||
let s = "Hello, world!";
|
||||
println!("{}", s.to_string());
|
||||
}
|
10
tests/ui/to_string_in_format_args_incremental.stderr
Normal file
10
tests/ui/to_string_in_format_args_incremental.stderr
Normal file
@ -0,0 +1,10 @@
|
||||
error: `to_string` applied to a type that implements `Display` in `println!` args
|
||||
--> $DIR/to_string_in_format_args_incremental.rs:8:21
|
||||
|
|
||||
LL | println!("{}", s.to_string());
|
||||
| ^^^^^^^^^^^^ help: remove this
|
||||
|
|
||||
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user