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

Add a lint against never type fallback affecting unsafe code #123939

Merged
merged 7 commits into from May 2, 2024

Conversation

WaffleLapkin
Copy link
Member

@WaffleLapkin WaffleLapkin commented Apr 14, 2024

I'm not very happy with the code quality... VecGraph not allowing you to get predecessors is very annoying. This should work though, so there is that. (ended up updating VecGraph to support getting predecessors)

First few commits are from #123934 #123980

@rustbot
Copy link
Collaborator

rustbot commented Apr 14, 2024

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 14, 2024
@rustbot
Copy link
Collaborator

rustbot commented Apr 14, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in coverage instrumentation.

cc @Zalathar

@rust-log-analyzer

This comment has been minimized.

@@ -4179,6 +4180,49 @@ declare_lint! {
"named arguments in format used positionally"
}

declare_lint! {
/// The `never_type_fallback_flowing_into_unsafe` lint detects cases where never type fallback
Copy link
Member

@Nilstrieb Nilstrieb Apr 14, 2024

Choose a reason for hiding this comment

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

the lint description should mention what never type fallback actually is, I think most people are not aware of it at all

Copy link
Member Author

Choose a reason for hiding this comment

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

That's one of the annoying problems of all the never type stuff, fallback can't be explained without going somewhat indepth :(

Copy link
Member Author

Choose a reason for hiding this comment

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

I added an explanation, does it look good?

@WaffleLapkin
Copy link
Member Author

Thinking about VecGraph more: we'll need to have predecessors for another lint anyway. One of the lints @nikomatsakis suggested checks for coercions like this:

?F -> ?1
       ^
       |
      ?N

Where ?F is a diverging variable which experiences fallback and ?N is non-diverging variable.

The algorithm to actually check this is

  1. From the variable which is falling back do a DFS, recording all the nodes we see and sinks (variables which are not coerced to anything)
  2. From sinks do a backwards DFS recording all nodes which we did not record in the 1-st step
  3. The nodes recorded in the 2-nd step are the ones we need to lint against

But VecGraph does not have predecessors and so can't do a backwards DFS.

This PR (and a future one for the third lint) are struggling/will struggle from a similar problem: we need to be able to go backwards (because this lint does not care about coercion direction -- since rust infers both ways).

I will try to use just Graph, but I'm not entirely sure its interface will work here... TBQH I wish rustc would just use an off-the-shelf library for graphs, instead of rolling its own half baked and undocumented version.

@WaffleLapkin
Copy link
Member Author

Okay. VecGraph actually exists for a reason -- having indices and edges upfront simplifies things quite a bit. Graph won't work nicely here, because we'd have to keep TyVid <-> NodeIndex map.

I'll try adding opt-in backreferences to VecGraph.

@bors
Copy link
Contributor

bors commented Apr 16, 2024

☔ The latest upstream changes (presumably #124026) made this pull request unmergeable. Please resolve the merge conflicts.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Apr 18, 2024
…r, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? `@fmease`

needed for rust-lang#123939
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Apr 18, 2024
…r, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? ``@fmease``

needed for rust-lang#123939
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Apr 18, 2024
…r, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? `@fmease`

needed for rust-lang#123939
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Apr 18, 2024
…r, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? ``@fmease``

needed for rust-lang#123939
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Apr 19, 2024
…r, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? ```@fmease```

needed for rust-lang#123939
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2024
Rollup merge of rust-lang#123980 - WaffleLapkin:graph-average-refactor, r=wesleywiser

 Add an opt-in to store incoming edges in `VecGraph` + misc

r? ```@fmease```

needed for rust-lang#123939
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the never-fallback-unsafe-lint branch 2 times, most recently from cee445b to 2a14501 Compare April 19, 2024 18:08
@WaffleLapkin

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

r? compiler-errors

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Some style nits, and then requesting that you implement the method case too.

compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fallback.rs Outdated Show resolved Hide resolved
@WaffleLapkin
Copy link
Member Author

@compiler-errors I've added support for unsafe methods, alongside other unsafe things which may have a type infered from the environment (like pointer dereference, union access, etc). The change is annoyingly big, but yeah...

Comment on lines 102 to 108
hir_typeck_never_type_fallback_flowing_into_unsafe =
never type fallback affects this call to an `unsafe` function
never type fallback affects this {$reason ->
[call] call to an `unsafe` function
[union_field] union access
[deref] raw pointer dereference
[path] `unsafe` function
[method] call to an `unsafe` method
Copy link
Member

Choose a reason for hiding this comment

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

Split this into 5 slugs and do:

#[derive(Diagnostic)]
enum NeverTypeFallbackFlowingIntoUnsafe {
  #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]  
  Call,
  ...
}

And then

hir_typeck_never_type_fallback_flowing_into_unsafe_call = never type fallback affects this call to an `unsafe` function

https://github.com/projectfluent/fluent/wiki/Good-Practices-for-Developers#prefer-separate-messages-over-variants-for-ui-logic

Copy link
Member Author

Choose a reason for hiding this comment

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

I've kept the reason enum and then convert to the diagnostic, cause I find using errors::* until the literal "emit" somewhat weird. But LMK if I should just return the diagnostic from the "find unsafe infer vars" function.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

r=me after splitting the fluent slugs

@compiler-errors compiler-errors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2024
@bors
Copy link
Contributor

bors commented Apr 30, 2024

☔ The latest upstream changes (presumably #124558) made this pull request unmergeable. Please resolve the merge conflicts.

@compiler-errors
Copy link
Member

that's fine

@bors r+

@bors
Copy link
Contributor

bors commented May 2, 2024

📌 Commit b562617 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 2, 2024
@bors
Copy link
Contributor

bors commented May 2, 2024

⌛ Testing commit b562617 with merge fcc06c8...

@bors
Copy link
Contributor

bors commented May 2, 2024

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing fcc06c8 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 2, 2024
@bors bors merged commit fcc06c8 into rust-lang:master May 2, 2024
7 checks passed
@rustbot rustbot added this to the 1.80.0 milestone May 2, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fcc06c8): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.3% [1.1%, 1.5%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 674.637s -> 671.059s (-0.53%)
Artifact size: 315.98 MiB -> 315.99 MiB (0.00%)

bors added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2024
…s, r=workingjubilee

Document never type fallback in `!`'s docs

Pulled the documentation I've written for rust-lang#123939.

I want a single place where never type fallback is explained, which can be referred in all the lints and migration materials.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants