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

Latest release bugs (v0.3.1506) missing unsafe errors : when using bit_flags and derive traits #14766

Closed
hnyls2002 opened this issue May 9, 2023 · 14 comments
Labels
C-support Category: support questions

Comments

@hnyls2002
Copy link

rust-analyzer version: rust-analyzer version: 0.3.1506-standalone

rustc version: rustc 1.64.0-nightly (f6f9d5e73 2022-08-04)

relevant settings:

This is my cargo

[package]
name = "os"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
bitflags = "1.2.1"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
xmas-elf = "0.7.0"

[features] 
default = ["larger_memory"]

# for larger memory to load app's elf file with debug symbols
larger_memory = []

# for loggers
NoneLog = []
Error = []
Warn = ["Error"]
Debug = ["Warn"]
Info = ["Debug"]
Trace = ["Info"]

[profile.release]
debug = true

Today when I open my vscode to check my repo, I found there are so many rust errors appears. They are all

this operation is unsafe and requires an unsafe function or block

which appear at

bitflags! {
    pub struct PTEFlags : usize{
        const V = 1 << 0; // valid
        const R = 1 << 1; // readable
        const W = 1 << 2; // writable
        const X = 1 << 3; // executable
        const U = 1 << 4; // user
        const G = 1 << 5; // global
        const A = 1 << 6; // accessed
        const D = 1 << 7; // dirty
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]

This is the shoot cut

image

image

I don't know why...

@hnyls2002 hnyls2002 added the C-support Category: support questions label May 9, 2023
@hnyls2002
Copy link
Author

The reporting errors are like these
image

@hnyls2002 hnyls2002 changed the title Missing unsafe errors : when using lazy_static and derive traits Latest release bugs (v0.3.1506) missing unsafe errors : when using lazy_static and derive traits May 9, 2023
@hnyls2002
Copy link
Author

When I rollback my rust-analyzer version from v0.3.1506 to v0.3.1498, this problem solved!

I think this could be a bug for the latest version released yesterday, even #[derive(Debug, Copy, Clone, Eq, PartialEq)] will cause errors...

@lnicola
Copy link
Member

lnicola commented May 9, 2023

Possibly caused by #14725, but it doesn't seem to happen every time.

@HKalbasi
Copy link
Member

HKalbasi commented May 9, 2023

Which trait(s) causes the error? Can someone post the result of Expand macro recursively on that trait?

@hnyls2002 hnyls2002 changed the title Latest release bugs (v0.3.1506) missing unsafe errors : when using lazy_static and derive traits Latest release bugs (v0.3.1506) missing unsafe errors : when using bit_flags and derive traits May 9, 2023
@hnyls2002
Copy link
Author

Sorry about my previous description about the macro, I just mixed up lazy_static with bit_flags, where the warnings show is actually the macro bit_flags but not lazy_static.

And I have checked the traits, I found when only Debug, Copy, Clone, there is no warning.

image

It seems that Eq, PartialEq, Ord, PartialOrd four traits causes the warning, and on each block the number of warnings are multiples of four.

image

Also, in the lazy_static macro, I clicked into it, and I also found that :

image

Maybe that's where the problems occur...

@HKalbasi
Copy link
Member

HKalbasi commented May 9, 2023

I can't reproduce this on lazy_static and bit_flags alone, and I'm not sure how to mix them. An standalone reproduction would help.

@Aeledfyr
Copy link

Aeledfyr commented May 10, 2023

I reproduced this just using slotmap's new_key_type, and then simplified it further:

#[derive(PartialOrd, Ord, PartialEq, Eq)]
struct Number(u32);

This fails with "this operation is unsafe and requires an unsafe function or block"

Expanding the Ord implementation results in this:

impl core::cmp::Ord for Number {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        match core::intrinsics::discriminant_value(self)
            .cmp(&core::intrinsics::discriminant_value(other))
        {
            core::cmp::Ordering::Equal => match (self, other) {
                (Number(f0_self), Number(f0_other)) => match f0_self.cmp(&f0_other) {
                    core::cmp::Ordering::Equal => core::cmp::Ordering::Equal,
                    c => return c,
                },
                _unused => core::cmp::Ordering::Equal,
            },
            c => return c,
        }
    }
}

And rust-analyzer is treating core::intrinsics::discriminant_value as unsafe. (Are the reproduction issues with rustc version / standard library source version mismatches?)

(I am using rust-analyzer 0.3.1506-standalone in vscode)

@hnyls2002
Copy link
Author

hnyls2002 commented May 10, 2023

I can't reproduce this on lazy_static and bit_flags alone, and I'm not sure how to mix them. An standalone reproduction would help.

Sorry to bother you, my English is not so good. When I mentioned I mixed lazy_static and bit_flags, I didn't mean that I mixed them in my code, but I just wrote the wrong name in this issue. ( The errors occur at bit_flags, and have nothing to do with lazy_static in my project)

@Veykril
Copy link
Member

Veykril commented May 10, 2023

This is indeed an issue with pre 1.69 rust versions, rust-lang/rust#100719 we rely on the safe intrinsic attribute which was introduced in 1.69 it seems

@Victor-N-Suadicani
Copy link

Just stumbled onto this as well. rustup update does indeed fix it, if you're in a position where you can upgrade your Rust version that is.

@Aeledfyr
Copy link

What version range of rust does rust-analyzer support? If it's only the latest stable then I guess this can stay closed, but otherwise this issue should be reopened, as the underlying issue is still there.

@Veykril
Copy link
Member

Veykril commented May 12, 2023

We usually only target the last 2-3 stable releases specifically. Sometimes it's only the latest though. If you can't upgrade your rust version for whatever reason we usually recommend sticking to an older r-a version until you can.

@kylejlin
Copy link

kylejlin commented Jun 3, 2023

When I rollback my rust-analyzer version from v0.3.1506 to v0.3.1498, this problem solved!

This worked for me too! Thanks for your help.

@Veykril
Copy link
Member

Veykril commented Jul 19, 2024

Closing this as its now certainly out of support range

@Veykril Veykril closed this as completed Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

7 participants