-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feature gate import shadowing #116
Conversation
(Update circa 15 July 2014: This comment was responding to an earlier version of the RFC which was less clear and thus seemed broader in scope. I'm leaving the comment in place so that one can understand the thread of discussion.) So, just to be clear: This would stop me from choosing certain names in a pattern binding (such as a fn parameter name) just because the same name happens to be pulled in from a use declaration (perhaps via a glob), is that right? One important case to make sure continues to work (and can work, even with this proposal in place; we just need to make sure it does work): if a macro introduces an item, it should not be restricted in the names it chooses for its hygienically injected bindings. For example, consider the following:
What I want to avoid is that the proposed rule be over-zealously interpreted as meaning that the The reason you need to make sure this case continues to work, i.e. the reason it is important that such psuedo-shadowing is allowed, is that if you disallowed such pseudo-shadowing, then macros are no longer proper abstractions, since any use of them becomes dependent upon all of their internally bound names being available. (Overall, as a Schemer, I prefer flexible shadowing rules rather than trying to restrict programmers from shadowing identifiers. But I think I do understand where people come from when they ask for things like this.) |
@pnkfelix I'm not sure what you mean with your first point about pattern bindings. |
@Kimundi I just said "pattern binding" to mean binding in general, i.e. via the declarations of a Its one thing if this RFC Is restricted to outlawing certain combinations of |
Ah, I see you think I'm proposing forbidding shadowing between any declaration in the same module. Thats not what I meant: I only propose forbidding shadowing of items (including view items) in the same flat scope. This would be forbidden: use foo::Bar;
struct Bar; But this would be allowed: use foo::Bar;
fn x() {
struct Bar;
} Thats what I mean with flat scope: There is the top level of the module, and then anonymous modules like function bodies that inherit all declarations from their parent, while also being able to shadow them themself, and currently So, bindings in Same situation with the macro: The |
Ah! That is indeed different than what I thought you were proposing. Okay. Maybe revise RFC to make that distinction explicit. |
+100, thank you for writing the proposal I was going to write. I'll try to recap how I understand this proposal, and how I think it should be, and I hope that these things coincide. DefinitionsScopeA scope is basically the space between braces, with inner braces starting a new scope. So:
NamespaceA separate namespace exists for each type of thing for which you can use the same identifier without conflict. Rust has separate namespaces for type-level things (types, traits), lifetimes, value-level things (variables, statics, functions, enum variants), modules, and macros. Item declarationItem declarations are basically those things which it is legal to declare outside of function bodies, so not The rules
MotivationItem declarations in a given scope are logically unordered, so it makes no sense for them to shadow each other. Allowing shadowing of declarations from outer scopes improves modularity, because it means that if additional declarations are introduced to an outer scope, the meaning of inner scopes is unaffected. |
Thanks for writing that up, your "rules" section is pretty much what I'm trying to propose here :) EDIT: |
Ah, the term "view item" was unfamiliar to me. But yes, by "item declarations" I meant both view items and definitions. |
This proposal seems reasonable to agree on. I'd be surprised if it even negatively affects existing code and I think it has the potential to make item visibilities less surprising, and if it turns out untenable we can relent at any time post-1.0.. |
@Kimundi can you revise the RFC to try to incorporate some of the feedback from the discussion here? |
@pnkfelix Yeah, but only around starting the weekend. |
@pnkfelix I updated the RFC with more information, I hope its now clearer what it is about. |
+1, I'm all about making resolve simpler. |
W.r.t. unresolved questions: Overlapping imports
I think forbidding this should be explicitly part of the RFC as well. Prelude
FWIW, what Haskell does here is that:
Rust doesn't have an equivalent for Prelude and backwards compatibilityHowever, the above doesn't solve the problem that adding names to the
My recommendation: Do none of these for now. There's no urgent need. Make shadowing things imported from the |
Somewhat related, I've specified a formalization (and prototype) of the name resolution rules I would eventually like to see Rust use: https://github.com/nikomatsakis/rust-name-resolution-algorithm It would be nice to make changes to resolve to make it forwards compatible with these rules. (That said, I do plan to amend those rules to take macros into account, and that has not been done.) |
Is there any good reason to have a feature gate at all? I think I would prefer to simply remove it. |
In meeting, we clarified that feature gate is just a means to do a more graceful deprecation, which makes sense. |
Discussed at https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2014-08-12.md. Accepted as RFC 50. Tracking bug: rust-lang/rust#16464 The feature gating is just going to be for the purposes of deprecation, with the intent to remove the feature completely in the future. |
Fix some typos.
Rendered View