Commit graph

667 commits

Author SHA1 Message Date
MoskalykA
feba680c39 Use method syntax 2024-10-26 09:39:04 +03:00
MoskalykA
41fa877362 Start using Option::is_none_or 2024-10-26 09:39:04 +03:00
Chayim Refael Friedman
4ac3dc1a2f Correctly resolve variables and labels from before macro definition in macro expansion
E.g.:
```rust
let v;
macro_rules! m { () => { v }; }
```

This was an existing bug, but it was less severe because unless the variable was shadowed it would be correctly resolved. With hygiene however, without this fix the variable is never resolved.
2024-10-22 21:49:17 +03:00
Chayim Refael Friedman
8adcbdcc49 Implement semitransparent hygiene
Or macro_rules hygiene, or mixed site hygiene. In other words, hygiene for variables and labels but not items.

The realization that made me implement this was that while "full" hygiene (aka. def site hygiene) is really hard for us to implement, and will likely involve intrusive changes and performance losses, since every `Name` will have to carry hygiene, mixed site hygiene is very local: it applies only to bodies, and we very well can save it in a side map with minor losses.

This fixes one diagnostic in r-a that was about `izip!()` using hygiene (yay!) but it introduces a huge number of others, because of #18262. Up until now this issue wasn't a major problem because it only affected few cases, but with hygiene identifiers referred by macros like that are not resolved at all. The next commit will fix that.
2024-10-22 21:26:56 +03:00
Lukas Wirth
c286786888
Merge pull request #18254 from ChayimFriedman2/fix-mut
fix: Nail destructuring assignment once and for all
2024-10-22 17:40:52 +00:00
bors
c58427ff94 Auto merge of #18371 - Veykril:veykril/push-kwttrusywysp, r=Veykril
fix: Fix incorrect parsing of use bounds

Fixes https://github.com/rust-lang/rust-analyzer/issues/18357
2024-10-22 11:42:11 +00:00
Lukas Wirth
95298a2e61 fix: Fix incorrect parsing of use bounds
Also lower them a bit more
2024-10-22 13:34:26 +02:00
Lukas Wirth
6c23f25e7f Fix new nightly lints 2024-10-22 11:48:41 +02:00
Laurențiu Nicola
a32039278f Replace some LayoutError variants with the rustc_abi errors 2024-10-22 10:19:25 +03:00
Laurențiu Nicola
250bf98d58 Merge from rust-lang/rust 2024-10-22 10:12:22 +03:00
Lukas Wirth
1cdc34fa4a Fix recursive_adt fixture 2024-10-21 11:28:18 +02:00
Noratrieb
6a2b8270c9 Update rustc-hash to version 2
This brings in the new optimized algorithm that was shown to have small performance benefits for
rustc.
2024-10-21 11:28:18 +02:00
roife
834ccbffba fix: classify safe as a contextual kw 2024-10-21 02:56:21 +08:00
Chayim Refael Friedman
2d4d6b678f Store patterns desugared from destructuring assignments in source map
And few more fixups.

I was worried this will lead to more memory usage since `ExprOrPatId` is double the size of `ExprId`, but this does not regress `analysis-stats .`. If this turns out to be a problem, we can easily use the high bit to encode this information.
2024-10-20 19:11:32 +03:00
Chayim Refael Friedman
61f162a43d Handle destructuring assignments uniformly
Instead of lowering them to `<expr> = <expr>`, then hacking on-demand to resolve them, we lower them to `<pat> = <expr>`, and use the pattern infrastructure to handle them. It turns out, destructuring assignments are surprisingly similar to pattern bindings, and so only minor modifications are needed.

This fixes few bugs that arose because of the non-uniform handling (for example, MIR lowering not handling slice and record patterns, and closure capture calculation not handling destructuring assignments at all), and furthermore, guarantees we won't have such bugs in the future, since the programmer will always have to explicitly handle `Expr::Assignment`.

Tests don't pass yet; that's because the generated patterns do not exist in the source map. The next commit will fix that.
2024-10-20 19:09:51 +03:00
roife
002f6ad6f1 fix: do not emit unsafe diagnositcs for safe statics in extern blocks 2024-10-20 19:49:57 +08:00
roife
9f1e450c4f feat: initial support for safe_kw in extern blocks 2024-10-20 17:12:52 +08:00
Stuart Cook
d6350f60f7
Rollup merge of #131942 - workingjubilee:reduce-haruspicy, r=lukas-code,lnicola
compiler: Adopt rust-analyzer impls for `LayoutCalculatorError`

We're about to massively churn the internals of `rustc_abi`. To minimize the immediate and future impact on rust-analyzer, as a subtree that depends on this crate, grow some API on `LayoutCalculatorError` that reflects their uses of it. This way we can nest the type in theirs, and they can just call functions on it without having to inspect and flatten-out its innards.
2024-10-20 14:06:04 +11:00
Jubilee Young
90ec8053c2 rust-analyzer: Nest LayoutCalculatorError in hir_ty::LayoutError 2024-10-19 11:09:24 -07:00
Lukas Wirth
c11b45a6bd internal: Add more trivially Sized types to is_sized check 2024-10-18 12:41:14 +02:00
Shoyu Vanilla
91293ea4d4 Do not consider match/let/ref of place that evaluates to ! to diverge, disallow coercions from them too 2024-10-15 00:37:40 +09:00
David Barsky
ccee36e8dd chore: rename salsa to ra_salsa 2024-10-14 10:09:22 -04:00
bors
9f1f5cd8f6 Auto merge of #18252 - ShoyuVanilla:issue-15799, r=Veykril
fix: Do not consider mutable usage of deref to `*mut T` as deref_mut

Fixes #15799

We are doing some heuristics for deciding whether the given deref is deref or deref_mut here;

5982d9c420/crates/hir-ty/src/infer/mutability.rs (L182-L200)

But this heuristic is erroneous if we are dereferencing to a mut ptr and normally those cases are filtered out here as builtin;

5982d9c420/crates/hir-ty/src/mir/lower/as_place.rs (L165-L177)

Howerver, this works not so well if the given dereferencing is double dereferencings like the case in the #15799.

```rust
struct WrapPtr(*mut u32);

impl core::ops::Deref for WrapPtr {
    type Target = *mut u32;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn main() {
    let mut x = 0u32;
    let wrap = WrapPtr(&mut x);
    unsafe {
        **wrap = 6;
    }
}
```

Here are two - outer and inner - dereferences here, and the outer dereference is marked as deref_mut because there is an assignment operation.
And this deref_mut marking is propagated into the inner dereferencing.
In the later MIR lowering, the outer dereference is filtered out as it's expr type is `*mut u32`, but the expr type in the inner dereference is an ADT, so this false-mutablility is not filtered out.

This PR cuts propagation of this false mutablilty chain if the expr type is mut ptr.
Since this happens before the resolve_all, it may have some limitations when the expr type is determined as mut ptr at the very end of inferencing, but I couldn't find simple fix for it 🤔
2024-10-14 12:07:31 +00:00
bors
574c89155b Auto merge of #18217 - ChayimFriedman2:cast-unknown-ptr, r=Veykril
fix: Comment out cast checks for unknown ptr kind

Just like we don't check for types containing unknown.

Fixes #18214.

See also https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/Another.20case.20of.20.2318064.3F.
2024-10-14 11:24:08 +00:00
Jake
9434ceba40
include fn prefix for all callable defs 2024-10-08 11:37:08 -07:00
Jake
5cba3e72bc
hir-ty: change struct constructor formatting.
before, when formatting struct constructor for `struct S(usize, usize)` it would format as:

    extern "rust-call" S(usize, usize) -> S

but after this change, we'll format as:

    fn S(usize, usize) -> S
2024-10-08 11:05:03 -07:00
Chayim Refael Friedman
13f853464c Use external stack in borrowck DFS
Because damnit, it can crash r-a. Why do people make this stupid DFSes anyway (I get it, it's easier until it blows).
2024-10-06 23:48:16 +03:00
Shoyu Vanilla
d3446a78a0 fix: Do not consider mutable usage of deref to *mut T as deref_mut 2024-10-07 01:49:14 +09:00
Chayim Refael Friedman
ff3c95f414 Comment out cast checks for unknown ptr kind
Just like we don't check for types containing unknown.
2024-09-30 19:26:16 +03:00
bors
7b60339273 Auto merge of #18207 - mbwilding:master, r=Veykril
fix: Ambiguity with CamelCase diagnostic messages, align with rustc warnings

Fixed diagnostic messages so they say UpperCamelCase rather than CamelCase, as it is ambiguous.
Usually I'd call it PascalCase, but in the code base it is called UpperCamelCase so I left it with that naming choice.

`rustc` says `upper camel case` also when the case is wrong
```
warning: trait `testThing` should have an upper camel case name
 --> src/main.rs:5:7
  |
5 | trait testThing {
  |       ^^^^^^^^^ help: convert the identifier to upper camel case: `TestThing`
  |
  = note: `#[warn(non_camel_case_types)]` on by default
```

This is in line with the UPPER_SNAKE_CASE diagnostic messages.
546339a7be/crates/hir-ty/src/diagnostics/decl_check.rs (L60)
546339a7be/crates/ide-diagnostics/src/handlers/incorrect_case.rs (L535)
2024-09-30 08:22:29 +00:00
Noah Bright
4255cae1bb Rename object_safety to dyn_compatibility
Up to a trait implemented by another package, linking to
$CARGO_HOME/registry/cache/index.crates.io-6f17d22bba15001f/
2024-09-29 07:26:45 -04:00
Matthew Wilding
60219d0b11
Fix ambiguity with CamelCase diagnostic messages 2024-09-29 16:35:37 +08:00
Laurențiu Nicola
6c24765cd4 Add more LayoutError variants 2024-09-25 09:15:11 +03:00
Laurențiu Nicola
37f7190b3e Merge from rust-lang/rust 2024-09-25 09:00:53 +03:00
Chayim Refael Friedman
141833850c Consider lifetime GATs object unsafe 2024-09-22 15:00:01 +03:00
Folkert de Vries
02f677372b add C-cmse-nonsecure-entry ABI 2024-09-21 13:04:14 +02:00
bors
bd59dc8e4c Auto merge of #18139 - ShoyuVanilla:issue-18109, r=Veykril
fix: Extend `type_variable_table` when modifying index is larger than the table size

Fixes #18109

Whenever we create an inference variable in r-a, we extend `type_variable_table` to matching size here;

f4aca78c92/crates/hir-ty/src/infer/unify.rs (L378-L381)

But sometimes, an inference variable is [created from chalk](ab710e0c9b/chalk-solve/src/infer/unify.rs (L743)) and passed to r-a as a type of an expression or a pattern.
If r-a set diverging flag to this before the table is extended to a sufficient size, it panics here;

f4aca78c92/crates/hir-ty/src/infer/unify.rs (L275-L277)

I think that extending table when setting diverging flag is reasonable becase we are already doing such extending to a size that covers the inference vars created from chalk and this change only covers the order-dependent random cases that this might fail
2024-09-18 20:01:38 +00:00
Shoyu Vanilla
b368f9125b fix: Extend type_variable_table when modifying index is larger than table size 2024-09-19 00:02:08 +09:00
bors
5b7c812634 Auto merge of #18136 - valadaptive:no-mangle-lints, r=Veykril
Don't lint names of #[no_mangle] extern fns

[Rust doesn't run the `non_snake_case_name` lint on `extern fn`s with the `#[no_mangle]` attribute](https://github.com/rust-lang/rust/pull/44966).

The conditions are:
- The function must be `extern` and have a `#[no_mangle]` attribute.
- The function's ABI must not be explicitly set to "Rust".

This PR replicates that logic here.
2024-09-18 08:50:40 +00:00
valadaptive
893f79270f Don't lint names of #[no_mangle] extern fns 2024-09-18 01:54:03 -04:00
Chayim Refael Friedman
f6eb5be591 Add diagnostics for unsafe_op_in_unsafe_fn
Turns out it's pretty easy, but I did have to add support for allowed-by-default lints.
2024-09-18 03:02:12 +03:00
Lukas Markeffsky
cd442a0577 assert that unexpectedly unsized fields are sized in the param env 2024-09-17 00:06:56 +02:00
Lukas Markeffsky
08344c2ae8 layout computation: eagerly error for unexpected unsized fields 2024-09-16 15:53:21 +02:00
bors
fd243cd0fb Auto merge of #18099 - ChayimFriedman2:diag-only-necessary, r=Veykril
Use more correct handling of lint attributes

The previous analysis was top-down, and worked on a single file (expanding macros). The new analysis is bottom-up, starting from the diagnostics and climbing up the syntax and module tree.

While this is more efficient (and in fact, efficiency was the motivating reason to work on this), unfortunately the code was already fast enough. But luckily, it also fixes a correctness problem: outline parent modules' attributes were not respected for the previous analysis. Case lints specifically did their own analysis to accommodate that, but it was limited to only them. The new analysis works on all kinds of lints, present and future.

It was basically impossible to fix the old analysis without rewriting it because navigating the module hierarchy must come bottom-up, and if we already have a bottom-up analysis (including syntax analysis because modules can be nested in other syntax elements, including macros), it makes sense to use only this kind of analysis.

Few other bugs (not fundamental to the previous analysis) are also fixed, e.g. overwriting of lint levels (i.e. `#[allow(lint)] mod foo { #[warn(lint)] mod bar; }`.

After this PR is merged I intend to work on an editor command that does workspace-wide diagnostics analysis (that is, `rust-analyzer diagnostics` but from your editor and without having to spawn a new process, which will have to analyze the workspace from scratch). This can be useful to users who do not want to enable check on save because of its overhead, but want to see workspace wide diagnostics from r-a (or to maintainers of rust-analyzer).

Closes #18086.
Closes #18081.
Fixes #18056.
2024-09-12 12:39:27 +00:00
Chayim Refael Friedman
4eb19df5e9 Use more correct handling of lint attributes
The previous analysis was top-down, and worked on a single file (expanding macros). The new analysis is bottom-up, starting from the diagnostics and climbing up the syntax and module tree.

While this is more efficient (and in fact, efficiency was the motivating reason to work on this), unfortunately the code was already fast enough. But luckily, it also fixes a correctness problem: outline parent modules' attributes were not respected for the previous analysis. Case lints specifically did their own analysis to accommodate that, but it was limited to only them. The new analysis works on all kinds of lints, present and future.

It was basically impossible to fix the old analysis without rewriting it because navigating the module hierarchy must come bottom-up, and if we already have a bottom-up analysis (including syntax analysis because modules can be nested in other syntax elements, including macros), it makes sense to use only this kind of analysis.

Few other bugs (not fundamental ti the previous analysis) are also fixed, e.g. overwriting of lint levels (i.e. `#[allow(lint)] mod foo { #[warn(lint)] mod bar; }`.
2024-09-12 15:24:38 +03:00
Chayim Refael Friedman
5c42c08518 Fix inference of literals when the expectation is Castable
I followed the compiler: 5bce6d48ff/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs (L1560-L1579).
2024-09-12 00:57:34 +03:00
Lukas Wirth
db04f514f2 Lift out workspace related data into a separate query to preserve crategraph deduplication 2024-09-11 12:16:41 +02:00
Shoyu Vanilla
569ac44daf Skip checks for cast to dyn traits 2024-09-11 01:40:13 +09:00
Lukas Wirth
7c5275939a fix: Properly prevent mir building with unknown types present 2024-09-06 14:44:05 +02:00
Lukas Wirth
20f7ab5ab4 fix: Always explicitly set trait ref self types when lowering 2024-09-06 14:06:41 +02:00