Skip to content

Support float-like tuple indices in offset_of!() #112216

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

Merged
merged 3 commits into from
Jun 9, 2023

Conversation

est31
Copy link
Member

@est31 est31 commented Jun 2, 2023

Supports invocations like offset_of!((((), ()), ()), 0.0). This 0.0 gets tokenized as float literal, so it has to be broken up again.

The code that did the breaking up was returning a finished Expr, while we need a Ident, so this PR splits up the parse_expr_tuple_field_access_float function into:

  • a function that breaks up the float literal (similar to TokenKind::break_two_token_op, but we do access the parser during this splitting operation, so we keep it as an inherent function on the parser)
  • and a function that constructs an Expr from it

The former we can then re-use in offset_of parsing. The edge cases especially involving whitespaces are tricky so this adds a bunch of new tests as well.

fixes #112204

@rustbot
Copy link
Collaborator

rustbot commented Jun 2, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@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 Jun 2, 2023
// Complex case: do all combinations of spacings because the spacing determines what gets
// sent to the lexer.
println!("{}", offset_of!(ComplexTup, 0.1.1.1));
println!("{}", builtin # offset_of(ComplexTup, 0. 1.1.1));
Copy link
Member Author

@est31 est31 Jun 2, 2023

Choose a reason for hiding this comment

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

Note that this isn't supported by the offset_of!() macro's declaration:

pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
    builtin # offset_of($Container, $($fields).+)
}

Does it make sense to change it to:

pub macro offset_of($Container:ty, $($fields:tt)+) {
    builtin # offset_of($Container, $($fields)+)
}

and then let the builtin syntax deal with it?

This pattern is supported for normal tuple field access.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm yeah that's a bit complicated to do because it means that the builtin offset_of needs to get way better when it comes to errors... maybe it would be good for a follow up PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the macro arguments weren't somewhat important as documentation, I'd rather go with

macro offset_of($($tt:tt)+) { ... }

and do all the work in the parser.

@est31 est31 force-pushed the offset_of_deep_tuple branch from 0793810 to c7b72af Compare June 2, 2023 23:16
@petrochenkov petrochenkov 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 Jun 8, 2023
Purely a refactor in preparation of using it in offset_of!() parsing
@est31 est31 force-pushed the offset_of_deep_tuple branch from c7b72af to 07a719a Compare June 8, 2023 21:40
The tokenizer gives us whole float literal tokens, we have to split them up
in order to be able to create field access from them.
@est31 est31 force-pushed the offset_of_deep_tuple branch from 07a719a to 1b90f5e Compare June 8, 2023 21:43
@est31 est31 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 8, 2023
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 9, 2023

📌 Commit 9fb266b has been approved by petrochenkov

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 Jun 9, 2023
@bors
Copy link
Collaborator

bors commented Jun 9, 2023

⌛ Testing commit 9fb266b with merge 43062c4...

@bors
Copy link
Collaborator

bors commented Jun 9, 2023

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 43062c4 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 9, 2023
@bors bors merged commit 43062c4 into rust-lang:master Jun 9, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 9, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (43062c4): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

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.9% [0.4%, 1.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.9% [0.4%, 1.3%] 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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.4% [-2.4%, -2.4%] 1
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) -2.4% [-2.4%, -2.4%] 1

Binary size

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

Bootstrap: 649.669s -> 649.217s (-0.07%)

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.

nested tuple field access in offset_of gets parsed as float
5 participants