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

Implement T-types suggested logic for perfect non-local impl detection #122747

Merged
merged 5 commits into from
Apr 5, 2024

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Mar 19, 2024

This implement T-types suggested logic for perfect non-local impl detection:

for each impl, instantiate all local types with inference vars and then assemble candidates for that goal, if there are more than 1 (non-private impls), it does not leak

This extension to the current logic is meant to address issues reported in #121621.

This PR also re-enables the lint non_local_definitions to warn-by-default.

Implementation was discussed in this zulip thread.

Fixes #121621
Fixes #121746

r? @lcnr (feel free to re-roll)

@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 Mar 19, 2024
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

i'd like some style changes, apart from this r=me after perf

compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
@lcnr
Copy link
Contributor

lcnr commented Mar 21, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 21, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 21, 2024
…=<try>

Implement T-types suggested logic for perfect non-local impl detection

This implement [T-types suggested logic](rust-lang#121621 (comment)) for perfect non-local impl detection:

> for each impl, instantiate all local types with inference vars and then assemble candidates for that goal, if there are more than 1 (non-private impls), it does not leak

This extension to the current logic is meant to address issues reported in rust-lang#121621.

This PR also re-enables the lint `non_local_definitions` to warn-by-default.

Implementation was discussed in this [zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/Implementing.20new.20non-local.20impl.20defs.20logic).

r? `@lcnr` *(feel free to re-roll)*
@bors
Copy link
Contributor

bors commented Mar 21, 2024

⌛ Trying commit eed8e95 with merge c5a16cd...

@bors
Copy link
Contributor

bors commented Mar 21, 2024

☀️ Try build successful - checks-actions
Build commit: c5a16cd (c5a16cd81a23f6f21bfed93b321b2d9f3420fe10)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c5a16cd): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +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)
2.2% [0.6%, 4.2%] 12
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.2% [0.6%, 4.2%] 12

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.3% [2.1%, 5.9%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.7% [0.6%, 3.4%] 7
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [0.6%, 3.4%] 7

Binary size

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

Bootstrap: 669.21s -> 669.518s (0.05%)
Artifact size: 312.83 MiB -> 315.05 MiB (0.71%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 21, 2024
@Urgau
Copy link
Member Author

Urgau commented Mar 21, 2024

I've looked locally (with callgrind) at the perf regression and it's the same symptom as in the first implementation #120393 (comment), the version of diesel (v1.4.8) we embed trigger the warnings more than 300 times leading to some unavoidable perf regression.

I have checked that at least from version 2.1.0 (May 2023) and onwards of the crate there is no warnings, I suspect there are no warnings in any >=2 of diesel (but could check due to MSRV issues).


One question through, I haven't update the diagnostic message, but I would like to do so before we merge this PR however I don't know what phrasing we should use, the one you proposed (@lcnr), seems a bit to "technical" for users:

An item nested inside an expression-containing item (through any level of nesting) may not define an impl<T0..Tn> [Trait<P1..Pn> for] P0 if it has an observable impact outside of this item

Any suggestion for what to replace it instead ?

Also, should we suggest or mention the 2 or more impls rule ?

@Urgau Urgau force-pushed the non-local-defs_perfect_impl branch from eed8e95 to 3c8cadc Compare March 21, 2024 18:19
@lcnr
Copy link
Contributor

lcnr commented Mar 22, 2024

something like the following 🤔

Impls inside of an item body should not impact code outside of that item.

and that this is the case if there are more than 1 other global impls is just an implementation detail imo

@Urgau Urgau force-pushed the non-local-defs_perfect_impl branch from 3c8cadc to ba7f52d Compare March 23, 2024 11:45
@Urgau
Copy link
Member Author

Urgau commented Mar 23, 2024

I've addressed all the review comments (expect for #122747 (comment)), as well as adding a conservative message for "not impacting code outside the item".

I believe this is ready for another review or approval (which I cannot do since I don't have r+ rights).

@Urgau
Copy link
Member Author

Urgau commented Mar 30, 2024

@lcnr, all the reviews comments have been addressed, but I can't r=you since I don't have r+ rights.
could you give a quick look at the PR again, thks

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

final nits, then r=me

compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
tests/ui/lint/non_local_definitions.rs Show resolved Hide resolved
@@ -443,7 +443,7 @@ lint_non_local_definitions_impl = non-local `impl` definition, they should be av
[one] `{$body_name}`
*[other] `{$body_name}` and up {$depth} bodies
}
.non_local = an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
.non_local = an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block and should not impact code outside of that item
Copy link
Contributor

Choose a reason for hiding this comment

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

that sentence seems off to me 🤔

Suggested change
.non_local = an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block and should not impact code outside of that item
.non_local = an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`

or even ignoring the second sentence, simply using " an impl definition is non-local if it is nested inside an item and may impact type checking outside of that item."

Copy link
Member Author

Choose a reason for hiding this comment

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

The sentence also seemed a bit weird to me. Thanks for the suggestions, I ended up using the first one, since it gives a bit more details.

compiler/rustc_lint/src/non_local_def.rs Outdated Show resolved Hide resolved
}

let const_anon = if self.body_depth == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

please explain what this if chain is doing? getting the span for an anon const suggestion?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's getting the span of the parent const item ident (if there is one) for the const anon suggestion.

I changed to variable name to span_for_const_anon_suggestion and added a comment to explain what it is doing.

@lcnr
Copy link
Contributor

lcnr commented Apr 5, 2024

@bors delegate+

@bors
Copy link
Contributor

bors commented Apr 5, 2024

✌️ @Urgau, you can now approve this pull request!

If @lcnr told you to "r=me" after making some further change, please make that change, then do @bors r=@lcnr

@Urgau Urgau force-pushed the non-local-defs_perfect_impl branch from ba7f52d to 2f2d5cc Compare April 5, 2024 17:26
@Urgau
Copy link
Member Author

Urgau commented Apr 5, 2024

@bors r=@lcnr

@bors
Copy link
Contributor

bors commented Apr 5, 2024

📌 Commit 2f2d5cc has been approved by lcnr

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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2024
@bors
Copy link
Contributor

bors commented Apr 5, 2024

⌛ Testing commit 2f2d5cc with merge 9d79cd5...

@bors
Copy link
Contributor

bors commented Apr 5, 2024

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing 9d79cd5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 5, 2024
@bors bors merged commit 9d79cd5 into rust-lang:master Apr 5, 2024
12 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 5, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9d79cd5): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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)
2.3% [0.6%, 4.4%] 12
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.4%, -0.2%] 9
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [-0.4%, 4.4%] 21

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.2% [-3.8%, -2.6%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.2% [-3.8%, -2.6%] 2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.7% [1.2%, 3.5%] 7
Regressions ❌
(secondary)
3.1% [3.1%, 3.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [1.2%, 3.5%] 7

Binary size

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

Bootstrap: 667.559s -> 670.251s (0.40%)
Artifact size: 318.06 MiB -> 318.26 MiB (0.06%)

@Urgau
Copy link
Member Author

Urgau commented Apr 5, 2024

The perf regressions in diesel are due to the lint being triggered and producing nearly 300 warnings (with 155 actually shown). See #122747 (comment) and #120393 (comment) for more details.

@rustbot label: +perf-regression-triaged

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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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
7 participants