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

expand: Simplify expansion of derives #65252

Merged
merged 4 commits into from
Oct 19, 2019

Conversation

petrochenkov
Copy link
Contributor

And make it more uniform with other macros.
This is done by merging placeholders for future derives' outputs into the derive container's output fragment early (addressing FIXMEs from #63667).

Also, macros with names starting with _ are no longer reported as unused, in accordance with the usual behavior of unused lints.

r? @matthewjasper or @mark-i-m

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 9, 2019
@petrochenkov
Copy link
Contributor Author

FWIW, the end goal here is attempting to turn derive into a regular attribute macro, but there's still quite a bit of steps until that:

  • Expanding derives immediately with their container instead of enqueueing them for later expansion. Maybe I'll do that in this PR if it stays unmerged for a few days.
  • Implementing left-to-right attribute expansion (cc @CAD97). Right now derives (perhaps from multiple derive containers) are always expanded after attributes, regardless of their order in source code, that's unnatural and is a source of some hacks. We should try to fix it, thankfully there is some future proofing in place.

Copy link
Member

@mark-i-m mark-i-m left a comment

Choose a reason for hiding this comment

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

Overall, it looks good to me and makes sense, but I don't feel confident enough in my understanding of derive macros to give a r=me...

src/libsyntax/ext/expand.rs Outdated Show resolved Hide resolved
src/librustc_resolve/build_reduced_graph.rs Show resolved Hide resolved
for placeholder in extra_placeholders {
def_collector.visit_macro_invoc(*placeholder);
}

let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope };
fragment.visit_with(&mut visitor);
Copy link
Member

Choose a reason for hiding this comment

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

Just to test my understanding, this visit now takes care of the derive placeholders, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the placeholders for derives are now inside the fragment, like placeholders for other kinds of macros.

@matthewjasper
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 10, 2019

📌 Commit 1270140 has been approved by matthewjasper

@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 Oct 10, 2019
tmandry added a commit to tmandry/rust that referenced this pull request Oct 11, 2019
…hewjasper

expand: Simplify expansion of derives

And make it more uniform with other macros.
This is done by merging placeholders for future derives' outputs into the derive container's output fragment early (addressing FIXMEs from rust-lang#63667).

Also, macros with names starting with `_` are no longer reported as unused, in accordance with the usual behavior of `unused` lints.

r? @matthewjasper or @mark-i-m
tmandry added a commit to tmandry/rust that referenced this pull request Oct 11, 2019
Rollup of 16 pull requests

Successful merges:

 - rust-lang#64337 (libstd: Fix typos in doc)
 - rust-lang#64986 (Function pointers as const generic arguments)
 - rust-lang#65048 (Added doc about behavior of extend on HashMap)
 - rust-lang#65191 (Add some regression tests)
 - rust-lang#65200 (Add ?Sized bound to a supertrait listing in E0038 error documentation)
 - rust-lang#65205 (Add long error explanation for E0568)
 - rust-lang#65240 (self-profiling: Add events for metadata loading (plus a small dep-tracking optimization))
 - rust-lang#65248 (Suggest `if let` on `let` refutable binding)
 - rust-lang#65252 (expand: Simplify expansion of derives)
 - rust-lang#65263 (Deduplicate is_{freeze,copy,sized}_raw)
 - rust-lang#65265 (Cleanup librustc mir err codes)
 - rust-lang#65266 (Mark Path::join as must_use)
 - rust-lang#65276 (Don't cc rust-lang/compiler for toolstate changes)
 - rust-lang#65277 (Query generator kind for error reporting)
 - rust-lang#65283 (stability: Do not use `buffer_lint` after lowering to HIR)
 - rust-lang#65289 (Fix suggested bound addition diagnostic)

Failed merges:

r? @ghost
Centril added a commit to Centril/rust that referenced this pull request Oct 11, 2019
Rollup of 16 pull requests

Successful merges:

 - rust-lang#64337 (libstd: Fix typos in doc)
 - rust-lang#64986 (Function pointers as const generic arguments)
 - rust-lang#65048 (Added doc about behavior of extend on HashMap)
 - rust-lang#65191 (Add some regression tests)
 - rust-lang#65200 (Add ?Sized bound to a supertrait listing in E0038 error documentation)
 - rust-lang#65205 (Add long error explanation for E0568)
 - rust-lang#65240 (self-profiling: Add events for metadata loading (plus a small dep-tracking optimization))
 - rust-lang#65248 (Suggest `if let` on `let` refutable binding)
 - rust-lang#65252 (expand: Simplify expansion of derives)
 - rust-lang#65263 (Deduplicate is_{freeze,copy,sized}_raw)
 - rust-lang#65265 (Cleanup librustc mir err codes)
 - rust-lang#65266 (Mark Path::join as must_use)
 - rust-lang#65276 (Don't cc rust-lang/compiler for toolstate changes)
 - rust-lang#65277 (Query generator kind for error reporting)
 - rust-lang#65283 (stability: Do not use `buffer_lint` after lowering to HIR)
 - rust-lang#65289 (Fix suggested bound addition diagnostic)

Failed merges:

r? @ghost
Centril added a commit to Centril/rust that referenced this pull request Oct 13, 2019
…hewjasper

expand: Simplify expansion of derives

And make it more uniform with other macros.
This is done by merging placeholders for future derives' outputs into the derive container's output fragment early (addressing FIXMEs from rust-lang#63667).

Also, macros with names starting with `_` are no longer reported as unused, in accordance with the usual behavior of `unused` lints.

r? @matthewjasper or @mark-i-m
Centril added a commit to Centril/rust that referenced this pull request Oct 14, 2019
…hewjasper

expand: Simplify expansion of derives

And make it more uniform with other macros.
This is done by merging placeholders for future derives' outputs into the derive container's output fragment early (addressing FIXMEs from rust-lang#63667).

Also, macros with names starting with `_` are no longer reported as unused, in accordance with the usual behavior of `unused` lints.

r? @matthewjasper or @mark-i-m
@bors
Copy link
Contributor

bors commented Oct 14, 2019

⌛ Testing commit 1270140 with merge 9f86b1832c885b9d61a34d977eb8ea2fc499faf9...

@rust-highfive
Copy link
Collaborator

Your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@bors
Copy link
Contributor

bors commented Oct 14, 2019

💔 Test failed - checks-azure

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 14, 2019
@pietroalbini
Copy link
Member

@bors retry azure network error

@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 Oct 14, 2019
@Centril
Copy link
Contributor

Centril commented Oct 14, 2019

@bors rollup=never (possible cause of failure in two rollups)

@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 Oct 15, 2019
@petrochenkov
Copy link
Contributor Author

rustdoc doesn't like macro items inside functions, which is weird because it was supposed to be fixed by #63624, I'll check further.

@petrochenkov
Copy link
Contributor Author

Removed one more unwrap from the function that #63624 previously fixed.
@bors r=matthewjasper

@bors
Copy link
Contributor

bors commented Oct 15, 2019

📌 Commit 487e3c1ca90d0bdc73ff479b4fda24d888b3cf54 has been approved by matthewjasper

@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 Oct 15, 2019
@bors
Copy link
Contributor

bors commented Oct 17, 2019

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 17, 2019
And make it more uniform with other macros.
By merging placeholders for future derives' outputs into the derive container's output fragment early.
The issue is rustdoc-specific because its root cause if the `everybody_loops` pass makes some def-ids to not have local hir-ids
@petrochenkov
Copy link
Contributor Author

@bors r=matthewjasper

@bors
Copy link
Contributor

bors commented Oct 18, 2019

📌 Commit 7f89f04 has been approved by matthewjasper

@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 Oct 18, 2019
@petrochenkov
Copy link
Contributor Author

@bors rollup=maybe

Centril added a commit to Centril/rust that referenced this pull request Oct 19, 2019
…hewjasper

expand: Simplify expansion of derives

And make it more uniform with other macros.
This is done by merging placeholders for future derives' outputs into the derive container's output fragment early (addressing FIXMEs from rust-lang#63667).

Also, macros with names starting with `_` are no longer reported as unused, in accordance with the usual behavior of `unused` lints.

r? @matthewjasper or @mark-i-m
bors added a commit that referenced this pull request Oct 19, 2019
Rollup of 6 pull requests

Successful merges:

 - #65174 (Fix zero-size uninitialized boxes)
 - #65252 (expand: Simplify expansion of derives)
 - #65485 (Suppress ICE when validators disagree on `LiveDrop`s in presence of `&mut`)
 - #65542 (Refer to "associated functions" instead of "static methods")
 - #65545 (More symbol cleanups)
 - #65576 (Don't add `argc` and `argv` arguments to `main` on WASI.)

Failed merges:

r? @ghost
@bors bors merged commit 7f89f04 into rust-lang:master Oct 19, 2019
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 7, 2021
expand/resolve: Turn `#[derive]` into a regular macro attribute

This PR turns `#[derive]` into a regular attribute macro declared in libcore and defined in `rustc_builtin_macros`, like it was previously done with other "active" attributes in rust-lang#62086, rust-lang#62735 and other PRs.
This PR is also a continuation of rust-lang#65252, rust-lang#69870 and other PRs linked from them, which layed the ground for converting `#[derive]` specifically.

`#[derive]` still asks `rustc_resolve` to resolve paths inside `derive(...)`, and `rustc_expand` gets those resolution results through some backdoor (which I'll try to address later), but otherwise `#[derive]` is treated as any other macro attributes, which simplifies the resolution-expansion infra pretty significantly.

The change has several observable effects on language and library.
Some of the language changes are **feature-gated** by [`feature(macro_attributes_in_derive_output)`](rust-lang#81119).

#### Library

- `derive` is now available through standard library as `{core,std}::prelude::v1::derive`.

#### Language

- `derive` now goes through name resolution, so it can now be renamed - `use derive as my_derive; #[my_derive(Debug)] struct S;`.
- `derive` now goes through name resolution, so this resolution can fail in corner cases. Crater found one such regression, where import `use foo as derive` goes into a cycle with `#[derive(Something)]`.
- **[feature-gated]** `#[derive]` is now expanded as any other attributes in left-to-right order. This allows to remove the restriction on other macro attributes following `#[derive]` (rust-lang/reference#566). The following macro attributes become a part of the derive's input (this is not a change, non-macro attributes following `#[derive]` were treated in the same way previously).
- `#[derive]` is now expanded as any other attributes in left-to-right order. This means two derive attributes `#[derive(Foo)] #[derive(Bar)]` are now expanded separately rather than together. It doesn't generally make difference, except for esoteric cases. For example `#[derive(Foo)]` can now produce an import bringing `Bar` into scope, but previously both `Foo` and `Bar` were required to be resolved before expanding any of them.
- **[feature-gated]** `#[derive()]` (with empty list in parentheses) actually becomes useful. For historical reasons `#[derive]` *fully configures* its input, eagerly evaluating `cfg` everywhere in its target, for example on fields.
Expansion infra doesn't do that for other attributes, but now when macro attributes attributes are allowed to be written after `#[derive]`, it means that derive can *fully configure* items for them.
    ```rust
	#[derive()]
	#[my_attr]
	struct S {
		#[cfg(FALSE)] // this field in removed by `#[derive()]` and not observed by `#[my_attr]`
		field: u8
	}
    ```
- `#[derive]` on some non-item targets is now prohibited. This was accidentally allowed as noop in the past, but was warned about since early 2018 (rust-lang#50092), despite that crater found a few such cases in unmaintained crates.
- Derive helper attributes used before their introduction are now reported with a deprecation lint. This change is long overdue (since macro modularization, rust-lang#52226 (comment)), but it was hard to do without fixing expansion order for derives. The deprecation is tracked by rust-lang#79202.
```rust
    #[trait_helper] // warning: derive helper attribute is used before it is introduced
    #[derive(Trait)]
    struct S {}
```

Crater analysis: rust-lang#79078 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants