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

Some comments and documentation for name resolution crate #48693

Merged
merged 2 commits into from
Mar 26, 2018
Merged

Some comments and documentation for name resolution crate #48693

merged 2 commits into from
Mar 26, 2018

Conversation

vorner
Copy link
Contributor

@vorner vorner commented Mar 3, 2018

Hello

I'm trying to get a grasp of how the name resolution crate works, as part of helping with rust-lang/rustc-dev-guide#16. Not that I'd be succeeding much, but as I was reading the code, I started to put some notes into it, to help me understand.

I guess I didn't get very far yet, but I'd like to share what I have, in case it might be useful for someone else. I hope these are correct (even if incomplete), but I'll be glad for a fast check in case I put something misleading there.

@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 @pnkfelix (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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 3, 2018
@pietroalbini
Copy link
Member

Thanks for this PR @vorner! We'll make sure someone from @rust-lang/compiler reviews this PR!

(@pnkfelix, ping from triage!)

@petrochenkov
Copy link
Contributor

Sorry, missed this. Will review.

@@ -662,6 +670,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
}
}

// This thing walks the whole crate in DFS manner, visiting each item, resolving names as it goes.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use doc comments. They'll show up on the impl in the docs.

/// Different [rib kinds](enum.RibKind) are transparent for different names.
///
/// The resolution keeps a separate stack of ribs as it traverses the AST for each namespace. When
/// resolving, the name is looked from inside out.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: looked up

@@ -1122,7 +1152,8 @@ impl<'a> NameBinding<'a> {
resolver.get_macro(self.def_ignoring_ambiguity())
}

// We sometimes need to treat variants as `pub` for backwards compatibility
// We sometimes need to treat variants as `pub` for backwards compatibility (eg. in enum
// variants)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change doesn't seem to clarify more. variants are already mentioned

}
}

// This is the interface through which the rest of the compiler asks about name resolution after
// the whole AST has been indexed.
Copy link
Contributor

Choose a reason for hiding this comment

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

again, this can be a doc comment

@vorner
Copy link
Contributor Author

vorner commented Mar 12, 2018

Good to know I can put a comment on trait implementation, I always thought it shows the description of the trait.

Should I squash the fixup once it looks good?

@oli-obk
Copy link
Contributor

oli-obk commented Mar 12, 2018

Yes, please squash the fixup. Lgtm now

@@ -909,7 +935,11 @@ enum PathResult<'a> {
}

enum ModuleKind {
/// Inline `mod something { ... }`.
Copy link
Contributor

@jseyfried jseyfried Mar 12, 2018

Choose a reason for hiding this comment

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

ModuleKind::Block is an anonymous module, i.e. a block:

fn main() {
    fn f() {} // (1)
    {
        f(); // This resolves to (2)
        fn f() {} // (2)
    }
}

Whether a module is inline or not has no effect on name resolution.

Block(NodeId),
/// Module from another file.
///
/// Also called a normal module in the following code.
Copy link
Contributor

Choose a reason for hiding this comment

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

A normal module is a mod module (inline or not). ModuleKind::Def could also be a trait or an enum.

@@ -1359,6 +1394,7 @@ pub struct Resolver<'a> {
injected_crate: Option<Module<'a>>,
}

/// Nothing really interesting here, it just provides memory for the rest of the crate.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: memory -> arenas

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is obvious it contains arenas, my point was that these arenas are there to allocate memory. Should I still change it?

}
}

/// This is the interface through which the rest of the compiler asks about name resolution after
/// the whole AST has been indexed.
Copy link
Contributor

Choose a reason for hiding this comment

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

hir::lowering::Resolver is used to resolve HIR paths generated due to desugaring during AST->HIR lowering, and to look up the resolution AST paths during lowering (get_resolution). After lowering, resolution information is embedded in the HIR; this is what the rest of the compiler uses. The Resolver is destroyed shortly after lowering.

@@ -864,6 +875,16 @@ enum RibKind<'a> {
}

/// One local scope.
///
/// A rib represents a scope names can live in. Note that these appear in many places, not just
/// around braces. At any place where the list of accessible names (of the given namespace)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably better to say it more explicitly - the list of accessible names changes or new restrictions on name accessibility are added.

@petrochenkov
Copy link
Contributor

r=me after addressing the comments

@petrochenkov petrochenkov 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 14, 2018
@shepmaster
Copy link
Member

Ping from triage @vorner ! You have pending feedback to address; will you be able to address it soon?

@vorner
Copy link
Contributor Author

vorner commented Mar 23, 2018

I know, I keep track of it. It seems the comments will need some more research on my side, so I postponed it. I hope to find the time during this weekend.

@vorner
Copy link
Contributor Author

vorner commented Mar 24, 2018

OK, added one more fixup commit.

@petrochenkov
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 25, 2018

📌 Commit 433a03e has been approved by petrochenkov

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 25, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Mar 26, 2018
…chenkov

Some comments and documentation for name resolution crate

Hello

I'm trying to get a grasp of how the name resolution crate works, as part of helping with rust-lang/rustc-dev-guide#16. Not that I'd be succeeding much, but as I was reading the code, I started to put some notes into it, to help me understand.

I guess I didn't get very far yet, but I'd like to share what I have, in case it might be useful for someone else. I hope these are correct (even if incomplete), but I'll be glad for a fast check in case I put something misleading there.
TimNN added a commit to TimNN/rust that referenced this pull request Mar 26, 2018
…chenkov

Some comments and documentation for name resolution crate

Hello

I'm trying to get a grasp of how the name resolution crate works, as part of helping with rust-lang/rustc-dev-guide#16. Not that I'd be succeeding much, but as I was reading the code, I started to put some notes into it, to help me understand.

I guess I didn't get very far yet, but I'd like to share what I have, in case it might be useful for someone else. I hope these are correct (even if incomplete), but I'll be glad for a fast check in case I put something misleading there.
bors added a commit that referenced this pull request Mar 26, 2018
Rollup of 7 pull requests

- Successful merges: #48693, #48932, #49103, #49170, #49187, #49346, #49353
- Failed merges:
@bors bors merged commit 433a03e into rust-lang:master Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants