Skip to content

view-types: store view types in the AST#156016

Open
scrabsha wants to merge 2 commits into
rust-lang:mainfrom
scrabsha:view-types/in-ast
Open

view-types: store view types in the AST#156016
scrabsha wants to merge 2 commits into
rust-lang:mainfrom
scrabsha:view-types/in-ast

Conversation

@scrabsha

@scrabsha scrabsha commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

View all comments

Tracking issue: #155938.

Instead of discarding view types, we store them in the AST now!

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 30, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from 52c4eee to 2bafb0c Compare May 1, 2026 13:55
@rust-log-analyzer

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from 2bafb0c to 7f6749b Compare May 1, 2026 15:17
@rustbot rustbot added T-clippy Relevant to the Clippy team. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels May 1, 2026
@scrabsha scrabsha force-pushed the view-types/in-ast branch from 7f6749b to 77bdbc2 Compare May 1, 2026 15:21
@rust-log-analyzer

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch 4 times, most recently from d9ef8d8 to ec789ac Compare May 3, 2026 09:31
Comment thread compiler/rustc_ast/src/ast.rs Outdated
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 3, 2026
rust-bors Bot pushed a commit that referenced this pull request May 3, 2026
view-types: store borrows of view types in the AST
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
@rust-bors

rust-bors Bot commented May 3, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9d3dea7 (9d3dea74651f758e35177070e510be8f3639a7fd, parent: 6769f690f947f12e36db6b6503bab265b7b2cced)

@rust-timer

This comment has been minimized.

@scrabsha scrabsha mentioned this pull request May 3, 2026
13 tasks
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9d3dea7): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.7% [-1.4%, -0.3%] 6
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.1%] 2
All ❌✅ (primary) -0.7% [-1.4%, -0.3%] 6

Max RSS (memory usage)

Results (primary 0.1%, secondary -2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.9% [1.9%, 1.9%] 1
Regressions ❌
(secondary)
1.2% [1.2%, 1.2%] 1
Improvements ✅
(primary)
-0.8% [-0.8%, -0.7%] 2
Improvements ✅
(secondary)
-4.4% [-7.3%, -1.5%] 2
All ❌✅ (primary) 0.1% [-0.8%, 1.9%] 3

Cycles

Results (secondary 3.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.0% [0.5%, 9.9%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.3% [-1.3%, -1.3%] 1
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 503.61s -> 496.571s (-1.40%)
Artifact size: 394.46 MiB -> 394.47 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 3, 2026
@rust-bors

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from ec789ac to 93208f2 Compare May 3, 2026 16:11
@scrabsha

scrabsha commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

^

r? nikomatsakis

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 17, 2026
@scrabsha

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@rustbot rustbot assigned TaKO8Ki and unassigned nikomatsakis May 17, 2026
@rust-log-analyzer

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from 1707ca0 to b65ae52 Compare May 17, 2026 16:34
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from b65ae52 to c916edc Compare May 17, 2026 16:41
@BoxyUwU

BoxyUwU commented Jun 11, 2026

Copy link
Copy Markdown
Member

r? compiler

@rustbot rustbot assigned dingxiangfei2009 and unassigned TaKO8Ki Jun 11, 2026
@BoxyUwU

BoxyUwU commented Jun 11, 2026

Copy link
Copy Markdown
Member

ah apparently oli was talked to privately about this,

r? oli-obk

@rustbot rustbot assigned oli-obk and unassigned dingxiangfei2009 Jun 11, 2026
Comment thread compiler/rustc_parse/src/parser/item.rs Outdated
let eself_hi = this.prev_token.span;
let eself = if this.eat(exp!(Colon)) {
SelfKind::Explicit(this.parse_ty()?, m)
let view = this.maybe_parse_view()?;

@oli-obk oli-obk Jun 17, 2026

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.

for pattern types I used a builtin macro instead of starting out with syntax. While the reason for that was that it was a breaking change due to the follow-rules of ty fragments allowing is after it. Is this not an issue for .?

Further advantages were that it's syntax that is "automatically" supported in r-a and syn, and is much easier to edit and fine tune

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

for pattern types I used a builtin macro instead of starting out with syntax. While the reason for that was that it was a breaking change due to the follow-rules of ty fragments allowing is after it. Is this not an issue for .?

Oh, nice catch. I'll switch to pattern_types!(Foo.{ fields }) immediately. Thanks for mentioning this!f

(For those at home who are trying to figure out how this can break code, #107606 (comment) is a good repro)

Further advantages were that it's syntax that is "automatically" supported in r-a and syn, and is much easier to edit and fine tune

Agreed. r-a screams at me every time i open my tests hehe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(writing this here so i avoid the pain of having to discover this again)

the same problem arises for self.{ fields } as well, for instance with this macro:

macro_rules! foo {
    ($it:item) => {};
    (impl Foo { fn bar(self.{ meow }) {} }) => {};
}

foo!(impl Foo { fn bar(self.{ meow }) {} });

we can't have nice things :(

@fmease fmease Jun 22, 2026

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 don't see how your example is problematic, it already doesn't compile, so it won't be a regression. However, it's true that the addition of the view types syntax did regress code.

E.g., the following snippet compiles on stable but it no longer compiles on beta & nightly:

macro_rules! m {
    ($ty:ty) => { /*compile_error!("");*/ };
    (&().{}) => {};
}

m!(&().{});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't see how your example is problematic, it already doesn't compile, so it won't be a regression.

is this another instance of #103534?

(ack for the rest of your message, i will fix it soon)

@scrabsha scrabsha force-pushed the view-types/in-ast branch from c916edc to 1f0b3bf Compare June 22, 2026 20:38
@rustbot

rustbot commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@scrabsha

Copy link
Copy Markdown
Contributor Author

^

@rustbot ready

@scrabsha scrabsha Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this file has been moved to tests/ui/view-types/feature-gate-view-types.rs (which is, AIUI what's done for pattern types)

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@scrabsha scrabsha force-pushed the view-types/in-ast branch from 1f0b3bf to 23bf586 Compare June 23, 2026 08:58
Comment thread src/tools/rustfmt/src/types.rs Outdated
Comment on lines +1058 to +1062
ast::TyKind::View(ref ty, ref fields) => {
let ty = ty.rewrite_result(context, shape)?;
let view = rewrite_view_fields(fields);
Ok(format!("{ty}.{view}"))
}

@ytmimi ytmimi Jun 23, 2026

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.

@scrabsha If we're going with the macro based approach do we still need rustfmt to format the new syntax? If it's behind a macro then rustfmt will see a macro call since it operates on the AST pre expansion.

We should handle these similar to type_ascribe!, include_bytes!, offset_of! which are all defined as builtin macros. When we see those in the AST we just return Err(RewriteError::Unknown)

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ytmimi thank you for pointing this out! i just pushed a fix for this, it should be good now :)

@scrabsha scrabsha Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

while i'm at it, @oli-obk, do you mind if i do the same thing for pattern types? (otherwise i can open a second PR just for this, it's your call)

(pattern types and view types are created using macros, i guess pattern types should go here too hehe)

@scrabsha scrabsha force-pushed the view-types/in-ast branch from 23bf586 to dcdf133 Compare June 23, 2026 20:05
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
...............................F.................. (50/144)
.................................................. (100/144)
............................................       (144/144)

======== tests/rustdoc-gui/go-to-collapsed-elem.goml ========

[ERROR] line 40
    at `tests/rustdoc-gui/go-to-collapsed-elem.goml` line 21: Error: Node is detached from document: for command `click: "//*[@id='search']//a[@href='../test_docs/struct.Foo.html#method.must_use']"`
    at <file:///checkout/obj/build/x86_64-unknown-linux-gnu/test/rustdoc-gui/doc/test_docs/struct.Foo.html?search=t_use>


<= doc-ui tests done: 143 succeeded, 1 failed, 0 filtered out

Error: ()

@scrabsha

scrabsha commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

i... don't really understand how my changes trigger this CI error? is the test being flaky?

@scrabsha

Copy link
Copy Markdown
Contributor Author

ok, it's probably linked to #93784

@bors try

@rust-bors

rust-bors Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

@scrabsha: 🔑 Insufficient privileges: not in try users

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. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.