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

Ambiguous "unsafe" Error WASM JS alert #5412

Closed
dgb23 opened this issue Jul 16, 2020 · 17 comments · Fixed by #9128
Closed

Ambiguous "unsafe" Error WASM JS alert #5412

dgb23 opened this issue Jul 16, 2020 · 17 comments · Fixed by #9128
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@dgb23
Copy link

dgb23 commented Jul 16, 2020

I'm in the process of following the Game of Life Rust-WASM tutorial here:

https://rustwasm.github.io/docs/book/game-of-life/hello-world.html

My level of expertise in Rust: Beginner

OS: macOS Catalina 10.15.5
Editor: VSCode 1.47
rust-analyzer: v0.2.240

(rust-analyzer updated just a few minutes ago)


I'm getting this error message when looking at src/lib.rs:

{
	"resource": "[...]/wasm-game-of-life/src/lib.rs",
	"owner": "_generated_diagnostic_collection_name_#1",
	"severity": 8,
	"message": "This operation is unsafe and requires an unsafe function or block",
	"source": "rust-analyzer",
	"startLineNumber": 18,
	"startColumn": 5,
	"endLineNumber": 18,
	"endColumn": 16
}

In this code block the second alert(...) is highlighted:

#[wasm_bindgen]
extern {
    fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    alert(name);
}

However the code compiles regardless.

When changing to:

#[wasm_bindgen]
pub fn greet(name: &str) {
    unsafe{alert(name);}
}

Then I'm getting this warning:

{
	"resource": "[...]/wasm-game-of-life/src/lib.rs",
	"owner": "_generated_diagnostic_collection_name_#1",
	"code": "unused_unsafe",
	"severity": 4,
	"message": "unnecessary `unsafe` block\n`#[warn(unused_unsafe)]` on by default",
	"source": "rustc",
	"startLineNumber": 18,
	"startColumn": 5,
	"endLineNumber": 18,
	"endColumn": 11
}
@jonas-schievink
Copy link
Contributor

That's just because we don't expand procedural macros yet. Presumably it does some magic to make these operations safe.

@dgb23
Copy link
Author

dgb23 commented Jul 16, 2020

That's just because we don't expand procedural macros yet. Presumably it does some magic to make these operations safe.

Would you rather recommend to get the "unsafe" error or the "unused unsafe" warning? Or in other words is my temporary 'fix' the right thing to do or should I just ignore it?

@jonas-schievink
Copy link
Contributor

I'd suggest listening to the rustc warning, not rust-analyzer. If the wrong diagnostics from rust-analyzer annoys you, you can also turn it off in the settings (but this currently turns all of them off).

bors bot added a commit that referenced this issue Aug 18, 2020
5682: Add an option to disable diagnostics r=matklad a=popzxc

As far as I know, currently it's not possible to disable a selected type of diagnostics provided by `rust-analyzer`.

This causes an inconvenient situation with a false-positive warnings: you either have to disable all the diagnostics, or you have to ignore these warnings.

There are some open issues related to this problem, e.g.: #5412, #5502

This PR attempts to make it possible to selectively disable some diagnostics on per-project basis.

Co-authored-by: Igor Aleksanov <popzxc@yandex.ru>
@kinnison
Copy link
Contributor

I just hit this today myself. Is there a timeline on rust-analyzer being able to run proc-macros?

@bjorn3
Copy link
Member

bjorn3 commented Aug 19, 2020

Derive proc macros already work when you enable them. The other kinds are not yet supported. I don't know of any timeline for imolementing this.

@jonas-schievink
Copy link
Contributor

The latest release has preliminary support for function-like procedural macros like foo!();.

Attribute macros are harder to implement and require pretty hairy name resolution changes.

@FermiDirak
Copy link

FermiDirak commented Sep 5, 2020

For those looking to disable the missing-unsafe rule until it's fixed and are using VS Code, adding the following to your settings.json and reloading your editor will suppress these errors:

"rust-analyzer.diagnostics.disabled": [
    "missing-unsafe"
]

credit to @popzxc for landing #5682, which makes the above possible

@digama0
Copy link
Contributor

digama0 commented Dec 3, 2020

Is it possible to disable these diagnostics locally, using an attribute like with clippy lints?

@jonas-schievink
Copy link
Contributor

Not yet, no

@flodiebold flodiebold added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Dec 22, 2020
jaburns added a commit to jaburns/dotfiles that referenced this issue Jan 14, 2021
@OliverEvans96
Copy link

I just ran into this issue with cxx, which uses macros to generate safe C++ FFIs.

@bjorn3
Copy link
Member

bjorn3 commented Jan 17, 2021

You can enable proc-macro support using

{
    "rust-analyzer.cargo.loadOutDirsFromCheck": true,
    "rust-analyzer.procMacro.enable": true,
}

@OliverEvans96
Copy link

Thanks @bjorn3, but unfortunately, that didn't get rid of the erroneous warning. I think I'll stick with disabling missing-unsafe for now.

@lnicola
Copy link
Member

lnicola commented Jan 17, 2021

@OliverEvans96 cxx also uses attribute proc macros, which are not supported.

@FrankenApps
Copy link

You can get rid of this error / warning locally by putting an unsafe block and then surpressing the warning, but it is not a great way to handle that:

#[wasm_bindgen]
pub fn greet(name: &str) {
    #[allow(unused_unsafe)]
    unsafe {
        alert(&format!("Hello, {}!", name));
    }
}

bors bot added a commit that referenced this issue Jun 3, 2021
9128: feat: expand procedural attribute macros r=jonas-schievink a=jonas-schievink

This adds experimental support for attribute macros. They can be enabled by setting `rust-analyzer.experimental.procAttrMacros` to `true`.

Known issues:
* Tokens aren't remapped, presumably because we edit the input syntax tree (this causes IDE features to not work inside items with attribute macros on them)
* Macro errors aren't reported correctly

Closes #8971
Fixes #8964 / la10736/rstest#120
Fixes #2984
Fixes #5412
Fixes #6029
Fixes #6687

#6740 is still not fixed – we now expand `#[proc_macro_hack]`, but fail to expand the resulting `proc_macro_call!()` macro.

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
@bors bors bot closed this as completed in 1415367 Jun 3, 2021
@PanopticaRising
Copy link

Just in case anyone doesn't think to check the merged PR and runs into this, I can confirm that I was able to set this in VS Code and got the warning to go away:

    "rust-analyzer.experimental.procAttrMacros": true,

@fosskers
Copy link

For folks using Doom Emacs, this seems to do the trick:

(after! lsp-rust
  (setq lsp-rust-analyzer-proc-macro-enable t
        lsp-rust-analyzer-experimental-proc-attr-macros t))

@lnicola
Copy link
Member

lnicola commented Oct 17, 2021

Recent rust-analyzer versions should have both of those options enabled.

martin-t added a commit to martin-t/rec-wars that referenced this issue Oct 17, 2021
Should be fixed or no longer applies - rust-lang/rust-analyzer#5412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

Successfully merging a pull request may close this issue.