slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.26k stars 581 forks source link

rust-analyzer error in the `slint!` macro when using dashes #685

Open ogoffart opened 2 years ago

ogoffart commented 2 years ago

Update: From Slint 1.3, we consider that dashes near identifier are always a single identifier with rust-analyzer.

The slint! macro need to make the difference between foo - bar and foo-bar. In order to do that, we use the Debug representation of Span because there is currently no stable API in Span that would allow to make the distinction. This works for rustc, but doesn't work for rust-analyzer that don't implement Debug for Span the right way.

So if we have a binding like

in-out property <int> foo-bar: self.width - self.y;

This will be an error with rust analyzer because it will see self.width-self.y and complain that width-self is not found. Workaround is to write it with parentheses: (self.width) - (self.y)


original description

When using the sixtyfps! macro, we get error if we use dashes in identifier.

That's because we use the debug representation of the proc_macro::Span to find out if tokens are next to each other or not. https://github.com/sixtyfpsui/sixtyfps/blob/844c5cf7ec222540a8799b6f29b981452d7bcf6c/api/sixtyfps-rs/sixtyfps-macros/lib.rs#L35-L38

We need that to make the difference between foo - bar and foo-bar

The problem is that rust analyzer is not providing any useful info in the Debug of the Span https://github.com/rust-analyzer/rust-analyzer/blob/91cb422e1a3cd96926af9203ab925ac6a90192b5/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs#L9 / https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs#L635-L637

We either need to find another way to know if two token are matching, like using the nightly feature Span::start / Span::end which i believe are also not implemented by rust-analyzer at the moment anyway. So the best is to try to get these stabilized somehow.

ogoffart commented 2 years ago

So basically depends on proc_macro::LineColumn being stabilized. Tracking issue: https://github.com/rust-lang/rust/issues/54725

ogoffart commented 1 year ago

Rust 1.66 will have Span::source_text that will help https://dev-doc.rust-lang.org/nightly/proc_macro/struct.Span.html#method.source_text

PgBiel commented 12 months ago

Hello, has there been any updates on this? I am getting no rust-analyzer completion on components when any hyphen is inserted in slint! (as if the macro call gets ignored). I get the error below on VSCode's "output" tab:

[ERROR hir_ty::mir] Overloaded deref on type {unknown} is not a projection

Perhaps an issue should be opened on upstream, if this isn't a problem with slint itself? :eyes:

Thanks in advance!

ogoffart commented 12 months ago

The problem is that Slint parses the Debug output of the proc_macro::Span to know if they are next to eachother, which is not future proof and unstable. rust-analyzer's doesn't have the same Debug output for Span.

We want to make the difference between foo-bar and foo - bar

What we'd need is ONE of this stabilized:

In the mean time, we could workaround that and say that if we can't parse the debug, we consider they touch eachother. Or we could do a search on the source_text of the whole macro to find out.

PgBiel commented 12 months ago

Perfect, thank you for the explanation! I'll wait patiently for things to get sorted out. In the meantime, I'll use .slint files, as that just works:tm:. :+1: