-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-trait-systemArea: Trait systemArea: Trait system
Milestone
Description
Right now, I think, an impl of trait T for type X is always visible from within a crate, but to be visible from outside a crate they must be public. I am frankly unsure if these rules are good or bad but I do believe they are to some extent accidental. We should decide when and if impls are visible.
For example, I could imagine any of the following rules:
- the rule we have now;
- impls of traits are always visible;
- impls of traits are subject to the typical visibility rules, meaning that if the impl is public in module A, it can only be used from modules that could import A.
Some related questions:
- currently coherence is enforced at the crate level. You can implement an impl for trait T and type X if it is T or X is defined in your crate. Module-level visibility has nothing to do with it.
- if we went with module-level visibility rules, does it interact with coherence at all? I guess it cannot, we still do not want to allow multiple impls of a given trait for a given type.
Metadata
Metadata
Assignees
Labels
A-trait-systemArea: Trait systemArea: Trait system
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
brson commentedon Oct 8, 2012
For consistency, enforcing normal module visibility rules on impls of traits makes sense, but I can't think of a reason it matters that much. From a library user's point of view, a private trait impl is the same as no trait impl.
The behavior of visibility on the anonymous impl should be considered too. I'm not sure if that's understood yet either or if it's assumed to be the same. Right now 'pub' does nothing on it I think.
My preference is that pub and priv everywhere mean module level visibility and everything is priv by default. Currently fields and methods are pub by default.
brson commentedon Oct 8, 2012
Making a
priv
trait impl does allow you to enforce some discipline on yourself as a library writer.With private by default impls you will frequently run into the problem of forgetting to make impls public, but I'm wary of more special cases.
What if we have a consistent priv by default policy everywhere, but offered policy control via attributes?
Dretch commentedon Oct 9, 2012
Is there a similar issue with the visibility of traits themselves? Currently they seem to always be visible within a crate, even if marked priv.
This can cause issues. For example:
Produces:
I found this surprising because I expected neither A nor B to be in scope.
OTOH this may in-fact be the same issue.
catamorphism commentedon Feb 21, 2013
Also see #3985
Format modules defined inside cfg_if macro calls (rust-lang#3600)
Auto merge of rust-lang#3600 - tiif:non-null-posix-memalign, r=RalfJung