Skip to content
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

Std and prelude support #7

Closed
HKalbasi opened this issue Aug 21, 2021 · 5 comments · Fixed by #8
Closed

Std and prelude support #7

HKalbasi opened this issue Aug 21, 2021 · 5 comments · Fixed by #8

Comments

@HKalbasi
Copy link
Member

I'm trying to add rust-analyzer-wasm to playground. Main problem here is that it doesn't support std. I ( code / demo ) currently added a fake std with subset of code as a simple dependency, but it has some problems:

  • It doesn't detect prelude. I added use std::prelude::v1::* and some more thing in first line invisibly. It works but it is hacky and there may be a better way.
  • It doesn't detect language features. For example std::ops::Range { } works but (1..num) type is detected as {unknown}

What I'm missing? Should I declare std crate special in rust-analyzer? Or some flags (I'm adding #[lang="Range"] but it doesn't work) in std source code? And in general what is the best way to add std to rust-analyzer-wasm?

@flodiebold
Copy link
Member

Something like this is indeed probably the simplest way, though ideally we could generate the fake std from the real one (we could replace all the function bodies by loop {} to keep it smaller).
The prelude should just be imported automatically as std::prelude::v1::*, if there is a dependency named std.
Ranges are currently desugared by known path, using core::ops::Range, so you'd need a core as well.

@HKalbasi
Copy link
Member Author

I added machine generated fakes of std, alloc and core and result looks cool. But there is a weird problem: if I add core as a direct dependency of main crate, it would detect core as prelude so Vec and similar won't work. and if I just add std as a dependency and add core as dependency of std, prelude becomes ok but 1..num problem returns.

Another problem, is that it doesn't detect Range<i32> implements Iterator. It doesn't have problem with Range<char> because Step for char is implemented normally but Step for i32 is implemented through a strange macro. I guess it is due my std is newer than ra0.0.44 so I tried to upgrade but it doesn't compile. I tested (latest-nightly, latest-stable, current-nightly)*(ra70, ra64) but I got errors | pattern need feature gate and url doesn't implement From<String>. Is it related to RA version? and if so which compiler version latest RA supports?

@flodiebold
Copy link
Member

I added machine generated fakes of std, alloc and core and result looks cool. But there is a weird problem: if I add core as a direct dependency of main crate, it would detect core as prelude so Vec and similar won't work. and if I just add std as a dependency and add core as dependency of std, prelude becomes ok but 1..num problem returns.

You need to add core; it should work if you add core, alloc and std in that order.

Another problem, is that it doesn't detect Range implements Iterator. It doesn't have problem with Range because Step for char is implemented normally but Step for i32 is implemented through a strange macro. I guess it is due my std is newer than ra0.0.44 so I tried to upgrade but it doesn't compile. I tested (latest-nightly, latest-stable, current-nightly)*(ra70, ra64) but I got errors | pattern need feature gate and url doesn't implement From. Is it related to RA version? and if so which compiler version latest RA supports?

Always latest stable.

@HKalbasi
Copy link
Member Author

You need to add core; it should work if you add core, alloc and std in that order.

Thanks, it works! I would never guess that.

Always latest stable.

I receive this error from latest stable:

error[E0277]: the trait bound `std::string::String: From<Url>` is not satisfied
   --> /home/hamid/.cargo/registry/src/github.com-1ecc6299db9ec823/ra_ap_ide-0.0.69/src/doc_links.rs:277:10
    |
277 |     Some(url.into())
    |          ^^^^^^^^^^ the trait `From<Url>` is not implemented for `std::string::String`
    |
    = help: the following implementations were found:
              <std::string::String as From<&'a js_sys::JsString>>
              <std::string::String as From<&mut str>>
              <std::string::String as From<&std::string::String>>
              <std::string::String as From<&str>>
            and 10 others
    = note: required because of the requirements on the impl of `Into<std::string::String>` for `Url`

It is maybe due to some cfg somewhere that doesn't work with wasm. I will try to find what is the problem and solving in upstream if possible, but I think it would be faster to try with an old std.

I see this as almost done, my goal is adding it to playground but I can file a PR here as well, if it is helpful. But it may be wanted to keep this demo simple.

@flodiebold
Copy link
Member

We make sure that cargo check -p ide --target=wasm32-unknown-unknown --all-targets works in CI, so I'm not sure why that would fail.

bors bot added a commit that referenced this issue Sep 14, 2021
8: Add std and inlay hints r=matklad a=HKalbasi

I actually done this as an experiment for adding [RA to playground](rust-lang/rust-playground#357) but I think this demo can also take advantage of this PR and by merging it, future people don't need to repeat my work.

Fix #7

This satisfies me, but it is not great. Specially it is slower than RA on vscode, and I don't know why it should be that slow. Suggestions on this (and other problems) are welcome.

Co-authored-by: hamidreza kalbasi <[email protected]>
Co-authored-by: HKalbasi <[email protected]>
bors bot added a commit that referenced this issue Sep 14, 2021
8: Add std and inlay hints r=matklad a=HKalbasi

I actually done this as an experiment for adding [RA to playground](rust-lang/rust-playground#357) but I think this demo can also take advantage of this PR and by merging it, future people don't need to repeat my work.

Fix #7

This satisfies me, but it is not great. Specially it is slower than RA on vscode, and I don't know why it should be that slow. Suggestions on this (and other problems) are welcome.

Co-authored-by: hamidreza kalbasi <[email protected]>
@bors bors bot closed this as completed in 03c4dc6 Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants