Skip to content

Conversation

@Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Nov 4, 2025

When lowering for loops, the spans for the into_iter call and the Some pattern used the span of the provided pattern and head expression. If either of those came from a different SyntaxContext this would result in some very strange contexts. e.g.:

macro_rules! m { ($e:expr) => { { $e } } }
for _ in m!(expr) {}

This would result in the into_iter call have a context chain of desugar => m!() => root which is completely nonsensical; m!() does not have a for loop. The into_iter call also ends up located at { $e } rather than inside the for _ in _ part.

This fixes that by walking the spans up to the for loop's context first. This will not handle adjusting the location of macro variable expansions (e.g. for _ in $e), but this does adjust the context to match the for loops.


This ended up causing rust-lang/rust-clippy#16008. Clippy should be using a debug_assert rather than unreachable, but it still results in a bug either way.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 4, 2025
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines +1778 to +1779
let head_span =
head.span.find_ancestor_in_same_ctxt(e.span).unwrap_or(head.span).with_ctxt(for_ctxt);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The currently failing tests/ui/suggest-dereferences/invalid-suggest-deref-issue127590 can be fixed by making a second mark rather than reusing for_ctxt. I'm not exactly clear why this matters since the only change this makes is using two identical, but separate SyntaxContexts.

Ideally only a single SyntaxContext would be created since there's only one for loop.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
failures:

---- [ui] tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.rs stdout ----

error: 2 diagnostics reported in JSON output but not expected in test file
tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.rs:6:24: ERROR: `&std::slice::Iter<'_, {integer}>` is not an iterator [E0277]
tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.rs:13:24: ERROR: `&std::slice::Iter<'_, {integer}>` is not an iterator [E0277]

thread '[ui] tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.rs' panicked at src/tools/compiletest/src/runtest.rs:922:13:
errors differ from expected
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/traits/suggest-dereferences/invalid-suggest-deref-issue-127590" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"

stack backtrace:
   5: __rustc::rust_begin_unwind
             at /rustc/3b4dd9bf1410f8da6329baa36ce5e37673cbbd1f/library/std/src/panicking.rs:698:5
   6: core::panicking::panic_fmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants