Skip to content

feat: initial support for safe_kw in extern blocks#18350

Merged
bors merged 2 commits into
rust-lang:masterfrom
roife:safe-kw-1
Oct 20, 2024
Merged

feat: initial support for safe_kw in extern blocks#18350
bors merged 2 commits into
rust-lang:masterfrom
roife:safe-kw-1

Conversation

@roife

@roife roife commented Oct 20, 2024

Copy link
Copy Markdown
Member

This PR adds initial support for safe keywords in external blocks.

Changes

  1. Parsing static declarations with safe kw and unsafe kw, as well as functions with safe kw in extern_blocks
  2. Add HAS_SAFE_KW to FnFlags
  3. Handle safe kw in is_fn_unsafe_to_call query
  4. Handle safe_kw in unsafe diagnostics

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 20, 2024
Comment thread crates/hir-def/src/item_tree.rs Outdated
@roife

roife commented Oct 20, 2024

Copy link
Copy Markdown
Member Author

This PR provides basic support to prevent r-a emitting syntax errors, and ensures that hovering does not display incorrect signatures. I create this PR first so that it can be merged before Monday.

Additional support such as completions and assists will be added in next few PRs.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Please add a test that unsafe diagnostics is not triggered for safe fn.

@roife

roife commented Oct 20, 2024

Copy link
Copy Markdown
Member Author

Tests for unsafe diagnostics have been added; also, I realized I forgot to add checks for safe statics, which have also been implemented in the new commit.

@lnicola

lnicola commented Oct 20, 2024

Copy link
Copy Markdown
Member

Thanks!

@bors r+

@bors

bors commented Oct 20, 2024

Copy link
Copy Markdown
Contributor

📌 Commit 002f6ad has been approved by lnicola

It is now in the queue for this repository.

@bors

bors commented Oct 20, 2024

Copy link
Copy Markdown
Contributor

⌛ Testing commit 002f6ad with merge 6dc5b32...

@bors

bors commented Oct 20, 2024

Copy link
Copy Markdown
Contributor

☀️ Test successful - checks-actions
Approved by: lnicola
Pushing 6dc5b32 to master...

Comment thread crates/hir-ty/src/utils.rs
Comment thread crates/ide-diagnostics/src/handlers/missing_unsafe.rs
pub mutable: bool,
pub is_extern: bool,
pub has_safe_kw: bool,
pub has_unsafe_kw: bool,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 bools is becoming noticeable. Can you check whether that increases the size (hover tells the size)? If then it's probably better to turn it into a bitflags (not necessary to do now though).

Also, why is has_unsafe_kw needed, given it defaults to unsafe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why is has_unsafe_kw needed, given it defaults to unsafe?

It seems to be used for pretty-printing?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we promise pretty-printing will recover exactly the original code, at least if we do there are bunch of cases this promise is broken.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'd argue it's better to always write unsafe in pretty printing since it may not be clear that the function is unsafe because it is extern (if we even show that it is extern, I don't remember now).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept both has_xxx_kw here in case we need them later, and it is similar to FnFlags. I also left a comment here: https://github.com/roife/rust-analyzer/blob/002f6ad6f1ca6aa59e1ebccc8478930101f8150f/crates/hir-def/src/item_tree.rs#L826-L829 that we may use bitflags in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one shouldn't have changed the size, but the next one (5th bool) might given alignment of 8 and other 4 byte fields in this struct.

I do think we should just swap all of these kinds of flags (this struct and others) to use bitflags, its more uniform that way and generally packs nicer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree we don't really need to preserve the explicit unsafe in our IRs here though, as in this case its not meaningful. But it doesn't really matter much either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants