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

Support an explicit annotation for marker traits #53693

Merged
merged 6 commits into from
Sep 25, 2018

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Aug 25, 2018

From the tracking issue for rust-lang/rfcs#1268:

It seems obvious that we should make a #[marker] annotation. ~ #29864 (comment)

This PR allows you to put #[marker] on a trait, at which point:

  • The trait must not have any items All of the trait's items must have defaults
  • Any impl of the trait must be empty (not override any items)
  • But impls of the trait are allowed to overlap

r? @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 25, 2018
@scottmcm scottmcm 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 Aug 25, 2018
@scottmcm scottmcm force-pushed the marker-trait-attribute branch 3 times, most recently from 3ed26ac to a8b137f Compare August 25, 2018 08:29
@rust-lang rust-lang deleted a comment from rust-highfive Aug 25, 2018
@scottmcm scottmcm force-pushed the marker-trait-attribute branch from ca490fe to fcd94f9 Compare August 25, 2018 11:52
@rust-lang rust-lang deleted a comment from rust-highfive Aug 25, 2018
@scottmcm scottmcm changed the title [WIP] Support an explicit annotation for marker traits Support an explicit annotation for marker traits Aug 26, 2018
@scottmcm
Copy link
Member Author

I think this is functional and sound, but it's missing the "this trait would be impossible to implement" error when defining a trait without default implementations for its items.

I'd appreciate advice on where that should go, as well as any other feedback on how I've been going about this.

@nikomatsakis
Copy link
Contributor

@scottmcm this is confusing to me. I thought a marker trait had no items -- not "items with defaults".

@scottmcm
Copy link
Member Author

@nikomatsakis See #29864 (comment) for some context. But yes, it's an extension beyond the RFC, so I'm fine not implementing that for now.

I do still need advice on where to put the "has no items" valuation check, though. (Once I know where I can figure out how to get the item list.)

@nikomatsakis
Copy link
Contributor

@scottmcm I see. That seems like an ok extension, actually, but in that case I don't know what this is referring to...

I think this is functional and sound, but it's missing the "this trait would be impossible to implement" error when defining a trait without default implementations for its items.

...oh, I see, I thought you meant some fancy check designed to prove that the trait is imposible to implement. But actually you just mean that you want to report an error if you label a trait as #[marker] and it has items that lack defaults?

In that case, I think we should do that .. hmm .. either in WF or in coherence. Coherence seems like -- today -- it is focused on checking properties of impls so maybe wfcheck would be a better place.

Basically extending this function:

fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
let trait_def_id = tcx.hir.local_def_id(item.id);
for_item(tcx, item).with_fcx(|fcx, _| {
check_where_clauses(tcx, fcx, item.span, trait_def_id, None);
vec![]
});
}

We should probably do a quick lang-team FCP here too.

@nikomatsakis nikomatsakis added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Aug 27, 2018
@nikomatsakis
Copy link
Contributor

@rfcbot fcp merge

This PR modifies the special treatment of marker traits in two ways. First, you have to declare the traits explicitly, by writing #[marker], but secondly, it permits items in those traits -- so long as those items have defaults. However, once a trait is defined as #[marker], impls of the trait are then disallowed from containing any items, so you know that the defaults from the trait will be used.

Seems reasonable to me.

@rfcbot
Copy link

rfcbot commented Aug 27, 2018

Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams:

Concerns:

Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 27, 2018
@withoutboats
Copy link
Contributor

@rfcbot concern vague

I'm 👍 on having a #[marker] annotation for marker traits with no items, and requiring that to get the overlapping allowance as a statement of your indefinite, permanent intent that this trait is a marker trait.

I'm not so convinced that redefining marker trait to include traits with default items and then adding additional checks that those are never overriden is a good idea. It comes at a nontrivial cost of complexity for users in terms of understanding the meaning and mechanics of both default methods and marker traits. The motivation doesn't seem that strong to me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 27, 2018
@Centril
Copy link
Contributor

Centril commented Aug 27, 2018

I very much agree that the incoherency should be opt-in rather than be implicit.

However; this was not discussed in the RFC (unless I somehow missed that but I did look at every comment...) and this could use some more elaborate and detailed justification.
For example, the changes wrt. provided definitions could use some more explaining per @withoutboats's concern. This is also, relatively speaking to the size of the original RFC, quite a large change.
Therefore I'd like to move this back to the drawing board, so:

@rfcbot concern needs-rfc

In addition; I am not so sure about the concrete syntax here.
For example, it seems to me that #[overlappable] (as suggested by @Vurich in #29864 (comment)) or #[incoherent] is more informative than #[marker] as to what effect it has. I think this holds particularly if associated items with defaults are permitted but can't be overridden as it isn't a marker trait anymore.

The choice of using an attribute rather than some keyword as a prefix to the trait (e.g. overlappable trait Foo { .. }) hasn't been sufficiently justified either in my opinion. The justification that has been given (#29864 (comment)) is merely that there is precedent to use an attribute. But precedent alone is not sufficient. In would like to see a discussion of this in an RFC. As such:

@rfcbot concern concrete-syntax

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 27, 2018
@Centril
Copy link
Contributor

Centril commented Aug 27, 2018

To elaborate a bit: I'm fine with landing this PR and using #[marker] as a temporary syntax; but not with resolving the final design at this stage.

@nrc
Copy link
Member

nrc commented Aug 27, 2018

I agree with @Centril and @withoutboats - this seems like a big change and marker traits with default methods adds considerably to the complexity of the feature, as well as making marker not really an appropriate name, IMO.

@nikomatsakis
Copy link
Contributor

@bors r-

@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-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Sep 17, 2018
@scottmcm scottmcm force-pushed the marker-trait-attribute branch from c991f11 to 3932249 Compare September 20, 2018 05:41
@scottmcm
Copy link
Member Author

@nikomatsakis Ok, how about something like this:

If you're using overlapping_marker_traits, please migrate your code to marker_trait_attr. The former has the problem that it makes adding a method to an empty trait a potentially-breaking change, so the latter requires that the trait opt-in to supporting overlapping implementations.

But both features continue to work and are unstable, so it's surprising to me that we'd relnote them at all.

@scottmcm scottmcm 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 Sep 22, 2018
@nikomatsakis
Copy link
Contributor

@bors r+

I'm not sure if we usually relnote unstable features. Seems like a nice thing to do.

@bors
Copy link
Contributor

bors commented Sep 24, 2018

📌 Commit 3932249 has been approved by nikomatsakis

@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 Sep 24, 2018
@bors
Copy link
Contributor

bors commented Sep 25, 2018

⌛ Testing commit 3932249 with merge 2871872...

bors added a commit that referenced this pull request Sep 25, 2018
Support an explicit annotation for marker traits

From the tracking issue for rust-lang/rfcs#1268:
> It seems obvious that we should make a `#[marker]` annotation. ~ #29864 (comment)

This PR allows you to put `#[marker]` on a trait, at which point:
- [x] The trait must not have any items ~~All of the trait's items must have defaults~~
- [x] Any impl of the trait must be empty (not override any items)
- [x] But impls of the trait are allowed to overlap

r? @nikomatsakis
@bors
Copy link
Contributor

bors commented Sep 25, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing 2871872 to master...

@bors bors merged commit 3932249 into rust-lang:master Sep 25, 2018
@scottmcm scottmcm deleted the marker-trait-attribute branch September 25, 2018 06:47
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 27, 2018
@Centril Centril added the F-marker_trait_attr `#![feature(marker_trait_attr)]` label Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-marker_trait_attr `#![feature(marker_trait_attr)]` finished-final-comment-period The final comment period is finished for this PR / Issue. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.