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

Implement RFC#1559: allow all literals in attributes #35850

Merged
merged 1 commit into from
Aug 30, 2016
Merged

Implement RFC#1559: allow all literals in attributes #35850

merged 1 commit into from
Aug 30, 2016

Conversation

SergioBenitez
Copy link
Contributor

Implemented rust-lang/rfcs#1559, tracked by #34981.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

continue
let name = word.name();
let message = match name {
Some(word) => match &*word {
Copy link
Member

Choose a reason for hiding this comment

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

This could probably be if let Some(word) = word.name() {.

Copy link
Member

Choose a reason for hiding this comment

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

Wait, doesn't .name() cause an error for repr(5) currently, that would be silenced?

Copy link
Contributor Author

@SergioBenitez SergioBenitez Aug 20, 2016

Choose a reason for hiding this comment

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

I'll use if let .... The check you're referring to happens here (libsyntax/attr.rs find_repr_attrs()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it's slightly cleaner to use match word.name(), so I'll switch it to that.

Copy link
Member

Choose a reason for hiding this comment

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

Might be able to do match word.name().as_ref().map_or("", |s| s) (or map_or("", |s| &**s)) - assuming that it has been previously validated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I considered that, but we don't know if it's a word at this point (though it doesn't matter too much, yet). Plus, it seems a bit more obvious to specifically match on the None case.

Copy link
Member

Choose a reason for hiding this comment

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

Two nested match are pretty ugly 😞.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to (from IRC):

            let name = match word.name() {
                Some(word) => word,
                None => continue,
            };

            let message = match &*name {
                ...
            }

            span_err!(self.sess, attr.span, E0517, "{}", message);

@eddyb
Copy link
Member

eddyb commented Aug 20, 2016

Looks pretty good to me, great job! 🎉

cc @nrc @cgswords @Manishearth

if meta_item.is_word() && id.is_none() {
id = Some(meta_item.name().clone());
} else {
// FIXME better-encapsulate meta_item (don't directly access `node`)
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum Aug 20, 2016

Choose a reason for hiding this comment

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

node is still directly accessed in the span_bug! so perhaps the FIXME shouldn't be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This happens in a lot of places without a similar comment. Is it more important to place such a comment here than in other places?

Copy link
Contributor

Choose a reason for hiding this comment

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

I added those fixmes; ultimately, there should be some encapsulation to make meta-items more opaque (so that their internal representation is less obvious and more resilient to changes). If it's done other places, this fixme should be added there, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll re-add them.

@cgswords
Copy link
Contributor

There should be some run-pass tests.

@cgswords
Copy link
Contributor

Aside from my comments and request for tests, this is basically all awesome. 🎉

@Manishearth
Copy link
Member

When this is ready to merge, let me know, we can batch up everything in #31645 (comment)

if let Some(mi) = item.meta_item() {
if mi.check_name(name) {
return Some(mi.clone())
}
Copy link
Member

Choose a reason for hiding this comment

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

Needing double ifs here is annoying - could check_name return None for non-meta items? (More generally, I kind of hate the concept of meta-item - it is arbitrary and meaningless. We should do our best not to expose the concept outside of libsyntax).

Copy link
Contributor Author

@SergioBenitez SergioBenitez Aug 22, 2016

Choose a reason for hiding this comment

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

Actually, we can call check_name directly on the list item. I'll change it to that.

Edit: Actually, we need to get the meta item somehow to return it. How do you feel about:

            for item in items.iter().flat_map(|l| l.iter()) {
                match item.meta_item() {
                    Some(mi) if mi.check_name(name) => return Some(mi.clone()),
                    _ => continue
                }
            }

@nrc
Copy link
Member

nrc commented Aug 22, 2016

This should be feature-gated, right?

@SergioBenitez
Copy link
Contributor Author

Why should this be feature gated? The syntax is backwards compatible, and custom attributes are nightly only.

@eddyb
Copy link
Member

eddyb commented Aug 22, 2016

@SergioBenitez So you're saying all stable attributes do checks that prevent new uses of literals?

@SergioBenitez
Copy link
Contributor Author

@eddyb: That's the intention with this PR, yeah.

span: span
}
})).collect()
v.into_iter().map(|(_, m)| sort_meta_item(m)).collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

This used to recursively sort MetaItemKind::Lists but now does nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

(now that they can contain literals, sorting MetaItemKind::Lists might not even make sense)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above.

@bors
Copy link
Contributor

bors commented Aug 25, 2016

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

@SergioBenitez
Copy link
Contributor Author

Rebased on master.

@nrc
Copy link
Member

nrc commented Aug 25, 2016

r+ with MetaListItem renamed and with the commits squashed. Thanks for the PR, awesome work!

@SergioBenitez
Copy link
Contributor Author

@nrc: Done.

@eddyb
Copy link
Member

eddyb commented Aug 25, 2016

@Manishearth r=nrc

@bors
Copy link
Contributor

bors commented Aug 25, 2016

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

@SergioBenitez
Copy link
Contributor Author

SergioBenitez commented Aug 25, 2016

@Manishearth: Rebased.

@nikomatsakis
Copy link
Contributor

@bors r=nrc

@bors
Copy link
Contributor

bors commented Aug 26, 2016

📌 Commit 8250a26 has been approved by nrc

@nikomatsakis
Copy link
Contributor

r? @nrc

@jseyfried
Copy link
Contributor

This will land in the next breaking batch.
@bors r-

@SergioBenitez
Copy link
Contributor Author

@jseyfried Wasn't this supposed to be part of the previous batch? Don't see the harm in landing it now if so.

@jseyfried
Copy link
Contributor

@SergioBenitez Which previous batch? Afaik, none of the pending syntax-[breaking-change] PRs have landed yet (c.f. #31645 (comment)).

@SergioBenitez
Copy link
Contributor Author

@jseyfried: #35979

@jseyfried
Copy link
Contributor

Those aren't syntax-[breaking-change]s though.

jseyfried added a commit to jseyfried/rust that referenced this pull request Aug 28, 2016
Implement RFC#1559: allow all literals in attributes

Implemented rust-lang/rfcs#1559, tracked by rust-lang#34981.
bors added a commit that referenced this pull request Aug 30, 2016
Batch up libsyntax breaking changes

Batch of the following syntax-[breaking-change] changes:
 - #35591: Add a field `span: Span` to `ast::Generics`.
 - #35618: Remove variant `Mod` of `ast::PathListItemKind` and refactor the remaining variant `ast::PathListKind::Ident` to a struct `ast::PathListKind_`.
 - #35480: Change uses of `Constness` in the AST to `Spanned<Constness>`.
  - c.f. `MethodSig`, `ItemKind`
 - #35728: Refactor `cx.pat_enum()` into `cx.pat_tuple_struct()` and `cx.pat_path()`.
 - #35850: Generalize the elements of lists in attributes from `MetaItem` to a new type `NestedMetaItem` that can represent a `MetaItem` or a literal.
 - #35917: Remove traits `AttrMetaMethods`, `AttributeMethods`, and `AttrNestedMetaItemMethods`.
  - Besides removing imports of these traits, this won't cause fallout.
 - Add a variant `Union` to `ItemKind` to future proof for `union` (c.f. #36016).
 - Remove inherent methods `attrs` and `fold_attrs` of `Annotatable`.
  - Use methods `attrs` and `map_attrs` of `HasAttrs` instead.

r? @Manishearth
@bors bors merged commit 8250a26 into rust-lang:master Aug 30, 2016
kevinmehall added a commit to kevinmehall/rust-peg that referenced this pull request Sep 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.