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

Identify non-function annotated by contract in macro expand #138852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Mar 23, 2025

Closing #137129

@rustbot
Copy link
Collaborator

rustbot commented Mar 23, 2025

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 23, 2025
@Noratrieb
Copy link
Member

Hm, I'm not entirely sure if this is the right fix. The contracts::ensures attribute must only be applied to functions, that the builtin macro expands when applied in this position in the first place is a bit strange.. it looks like this logic here is wrong:

.span_err(attr_span, "contract annotations can only be used on functions"));

I'd prefer to see the contract macros do proper parsing by fixing this FIXME here:
// FIXME: this kind of manual token tree munging does not have significant precedent among

instead of adding special cases for them

@Noratrieb Noratrieb 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 Mar 24, 2025
@xizheyin
Copy link
Contributor Author

Thank you, I will try to fix it in this way. @Noratrieb

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@xizheyin xizheyin changed the title Skip suggestion with equal spans attribute affected in suggest_trait_and_bounds Identify non-function annotated by contract in macro expand Mar 25, 2025
@xizheyin
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot 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 Mar 25, 2025
@Noratrieb
Copy link
Member

I don't think we should make this token parsing even more complicated than it already is. Either we do a proper AST macro like the other built-in attribute macros, or we'll leave this issue open. It's a very weird edge case, so it's not very important.
Doing this as a proper AST macro will probably not be very easy as it requires adding more features to the rustc expansion infrastructure (but it's certainly not impossible).

@xizheyin
Copy link
Contributor Author

I might have a try. Do you have any suggestions on implementation (such as things to pay attention to)? Or are there some codes and documents to refer to? I have no experience in builtin atrribute macro. @Noratrieb

@Noratrieb
Copy link
Member

I haven't looked much at the code myself, no. Something like the rustc_autodiff macro might be worth looking at, and how it works end-to-end. The rustc dev guide also has some docs on macro expansion. But as I said, it's probably not easy.

@xizheyin
Copy link
Contributor Author

Should I open an issue to track this?

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-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.

None yet

3 participants