Skip to content

type-mismatch returns an error on a correct use of the scan_fmt macro #13758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SeanGeb opened this issue Dec 10, 2022 · 1 comment · Fixed by #18264
Closed

type-mismatch returns an error on a correct use of the scan_fmt macro #13758

SeanGeb opened this issue Dec 10, 2022 · 1 comment · Fixed by #18264
Labels
A-macro macro expansion A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@SeanGeb
Copy link

SeanGeb commented Dec 10, 2022

rust-analyzer version: 0.3.1309-standalone (a2beeb8 2022-12-04); also affects pre-release v0.4.1314.
rustc version: rustc 1.65.0 (897e37553 2022-11-02)
relevant settings: None - I've used VSCode's rust-analyzer extension defaults everywhere, except for

{"rust-analyzer.inlayHints.typeHints.enable": false, "rust-analyzer.checkOnSave.command": "clippy"}

I've found a reproducible case where rust-analyzer returns a type mismatch error on code that compiles and runs correctly. (Caveat: I'm relatively new to the Rust ecosystem; equally, I can't see a case in which rustc and RA disagreeing about the type system isn't a bug!)

The offending code below is based on the example given in scan_fmt.

#[macro_use]
extern crate scan_fmt;

fn main() {
    let word = scan_fmt!("hello", "{}", String).unwrap();
    println!("{}", word);
}

The "problems" tab in VS Code contains the following:

expected String, found &str  rust-analyzer(type-mismatch)  [Ln 5, Col 16]
expected String, found &str  rust-analyzer(type-mismatch)  [Ln 5, Col 16]
expected String, found &str  rust-analyzer(type-mismatch)  [Ln 5, Col 16]

By comparison, clippy doesn't raise any errors, and compiling and running the example works fine:

PS C:\[...]\ra-bug-report> cargo run --bin repro
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target\debug\repro.exe`
hello

The RA error has the following verbose result:

[{
	"resource": "/c:/[...]/ra-bug-report/src/bin/repro.rs",
	"owner": "rustc",
	"code": {
		"value": "type-mismatch",
		"target": {
			"$mid": 1,
			"external": "https://rust-analyzer.github.io/manual.html#type-mismatch",
			"path": "/manual.html",
			"scheme": "https",
			"authority": "rust-analyzer.github.io",
			"fragment": "type-mismatch"
		}
	},
	"severity": 8,
	"message": "expected String, found &str",
	"source": "rust-analyzer",
	"startLineNumber": 5,
	"startColumn": 16,
	"endLineNumber": 5,
	"endColumn": 48
}]
@Veykril Veykril added A-ty type system / type inference / traits / method resolution A-macro macro expansion labels Dec 11, 2022
@flodiebold
Copy link
Member

This appears to be another case of #11681; scan_fmt generates code that has two different err variables distinguished by hygiene, where one is a String and the other a &str.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants