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

fix: Implement mixed site hygiene #18264

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

ChayimFriedman2
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 commented Oct 8, 2024

The realization that made me do this (as explained in detail in the first commit) is that while def-site hygiene is incredibly hard for us, mixed-site hygiene should be easy (well, I underestimated it due to the need of the second commit, but still). And the best part in that is that mixed-site hygiene rules: def-site hygiene is available only on nightly and without a clear path to stabilization, and even when (if) it will stabilize this will still help solving the case of variables/labels.

This brings diagnostics on self down to four (from five, I think? or more) 🎉 and the remaining diagnostics all require next solver (three due to coercion issue, one due to the issue of trait bounds not being preferred).

Best reviewed commit by commit.

Fixes #18262.
Fixes #16342.
Fixes #13758.
Fixes #11681.
Fixes #10535.

(Wow, so many issues! That's heart-filling ❤️)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2024
crates/hir-def/src/body.rs Show resolved Hide resolved
crates/hir-def/src/body/scope.rs Show resolved Hide resolved
crates/hir-def/src/expander.rs Outdated Show resolved Hide resolved
crates/hir-def/src/body/lower.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Oct 14, 2024

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

crates/hir-def/src/body/lower.rs Show resolved Hide resolved
crates/hir-expand/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Okay one more last thing, does this have a significant impact on memory (sorry, I'll keep asking this 😬), and does this have a noticable perf impact on the integrated_benchmark benches?

@@ -868,7 +920,9 @@ impl ExprCollector<'_> {
args: Box::new([self.collect_pat_top(e.pat())]),
ellipsis: None,
};
let label = e.label().map(|label| self.collect_label(label));
let label = e.label().map(|label| {
(self.hygiene_id_for(label.syntax().text_range().start()), self.collect_label(label))
Copy link
Member

Choose a reason for hiding this comment

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

            (self.hygiene_id_for(label.syntax().text_range().start()), self.collect_label(label))

(not relevant to this PR per se) The label.syntax().text_range().start() pattern is being done so often we really need to add a convenience function for fetching the ranges and stuff for typed ast nodes, the syntax() call is just noise

crates/hir-def/src/body/lower.rs Outdated Show resolved Hide resolved
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 rust-lang#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.
@ChayimFriedman2 ChayimFriedman2 force-pushed the semi-transparent branch 2 times, most recently from 206b13f to 345d698 Compare October 22, 2024 18:36
…n 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.
@ChayimFriedman2
Copy link
Contributor Author

@Veykril Addressed comments.

About perf impact:

This does not have a significant impact on memory (nor do I expect it to have after the removal of the MacroId from MacroDefId), around 5mb but that's in the unstable range.

For speed, the integrated benchmarks do not show a meaningful difference, but they are very flaky. If I benchmark the whole thing (including workspace loading) using hyperfine, it is up to 200ms slower, but I tend to think this should be attributed to the workspace loading and not the benchmark itself, if this is even a true regression and not just a measurement error..

analysis-stats . is not affected.

@Veykril Veykril added this pull request to the merge queue Oct 23, 2024
Merged via the queue into rust-lang:master with commit f9935be Oct 23, 2024
9 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the semi-transparent branch October 24, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment