rust-lang / rust-lang/rust-analyzer
`extract into function` misses scoped consts
Open
Nobody has claimed this yet.
A-assists
C-bug
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
rust-analyzer 1.75.0-nightly (e0d7ed1 2023-10-01)
rustc 1.75.0-nightly (e0d7ed1f4 2023-10-01)
binary: rustc
commit-hash: e0d7ed1f453fb54578cc96dfea859b0e7be15016
commit-date: 2023-10-01
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2
fn main() {
const A: &str = "HELLO";
let b = 123;
let c = vec![1, 2, 3, 4];
// extract into function start
c.iter().for_each(|x| {
if x > &b {
eprintln!("{}", A);
}
})
// extract into function end
}
extracting the marked part into a function will generate new function outside of main, which will still reference A which only exists inside main() and thus is out of scope and causes an error:
fn main() {
const A: &str = "HELLO";
let b = 123;
let c = vec![1, 2, 3, 4];
// extract into function start
fun_name(c, b);
// extract into function end
}
fn fun_name(c: Vec<i32>, b: i32) {
c.iter().for_each(|x| {
if x > &b {
eprintln!("{}", A); // welp
}
})
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the marked extract into function assist on the Rust snippet and inspect the generated function when A is a scoped const. Add regression coverage for this case, then verify that extraction preserves valid access to the const and the generated code compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100