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

resolve: Improve import failure detection and lay groundwork for RFC 1422 #32328

Merged
merged 10 commits into from
Apr 5, 2016

Conversation

jseyfried
Copy link
Contributor

This PR improves import failure detection and lays some groundwork for RFC 1422.
More specifically, it

  • Avoids recomputing the resolution of an import directive's module path.
  • Refactors code in resolve_imports that does not scale to the arbitrarily many levels of visibility that will be required by RFC 1422.
    • Replaces ModuleS's fields public_glob_count, private_glob_count, and resolved_globs with a list of glob import directives globs.
    • Replaces NameResolution's fields pub_outstanding_references and outstanding_references with a field single_imports of a newly defined type SingleImports.
  • Improves import failure detection by detecting cycles that include single imports (currently, only cycles of globs are detected). This fixes Improve name resolution failure detection #32119.

r? @nikomatsakis

@jseyfried
Copy link
Contributor Author

After this PR, the first coherence condition from this comment will hold.

};
(resolve_in_ns(ValueNS, value_determined.get()),
resolve_in_ns(TypeNS, type_determined.get()))
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The improved failure detection subsumes this hack.

@jseyfried
Copy link
Contributor Author

cc @petrochenkov @nrc

@nikomatsakis nikomatsakis self-assigned this Mar 25, 2016
@bors
Copy link
Contributor

bors commented Mar 26, 2016

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

@nikomatsakis
Copy link
Contributor

(note: starting reviewing, didn't yet finish)

None => return Some(Indeterminate),
};
let name = match directive.subclass {
SingleImport { source, target, .. } if source == target => target,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't quite follow this -- why are you checking if source == target? Can you maybe give an example of some Rust code that would trigger this path?

@nikomatsakis
Copy link
Contributor

looks good, though I'd like to learn the answer to my question :)

r=me with an extended comment

@jseyfried
Copy link
Contributor Author

I improved the design in the above commit so that the source == target check is no longer needed.

The check was needed since this code assumed that there was a cycle when we try to resolve a name in a module that is already borrowed (i.e. in which we are already trying to resolve a name). If we only follow globs and non-renamed single imports, this assumption is correct since the names are guaranteed to be the same. If we followed renamed single imports,

struct Bar;
use foo::Baz;
//^ here, we would try to resolve `foo::Baz`. Since `foo::Baz` is not yet successful,
//| we would follow the only single import that can define it (`use self::Bar as Baz`) and
//| try to resolve `foo::Bar`, which would incorrectly fail since `foo` is already borrowed
//| (causing the above import to incorrectly fail).
mod foo {
    use self::Bar as Baz;
    use Bar;
}

The above commit borrows the NameResolution directly when resolving a name (instead of borrowing its containing module) so that there are no "false positive" cycles even when following renamed single imports.
Following renamed single imports is necessary for the amended test to pass since otherwise we could not conclude that pub use super::T as S; fails in the value namespace.

@nikomatsakis
Copy link
Contributor

@jseyfried

I was always suspicious of that logic around cycle detection, makes sense.

@bors r+

@bors
Copy link
Contributor

bors commented Apr 4, 2016

📌 Commit 6f09dea has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Apr 5, 2016

⌛ Testing commit 6f09dea with merge 3562671...

@alexcrichton
Copy link
Member

@bors: retry force clean

@bors
Copy link
Contributor

bors commented Apr 5, 2016

⌛ Testing commit 6f09dea with merge 7fd331e...

bors added a commit that referenced this pull request Apr 5, 2016
resolve: Improve import failure detection and lay groundwork for RFC 1422

This PR improves import failure detection and lays some groundwork for RFC 1422.
More specifically, it
 - Avoids recomputing the resolution of an import directive's module path.
 - Refactors code in `resolve_imports` that does not scale to the arbitrarily many levels of visibility that will be required by RFC 1422.
  - Replaces `ModuleS`'s fields `public_glob_count`, `private_glob_count`, and `resolved_globs` with a list of glob import directives `globs`.
  - Replaces `NameResolution`'s fields `pub_outstanding_references` and `outstanding_references` with a field `single_imports` of a newly defined type `SingleImports`.
 - Improves import failure detection by detecting cycles that include single imports (currently, only cycles of globs are detected). This fixes #32119.

r? @nikomatsakis
@bors bors merged commit 6f09dea into rust-lang:master Apr 5, 2016
@jseyfried jseyfried deleted the coherence branch April 8, 2016 02:01
@jseyfried jseyfried mentioned this pull request Apr 8, 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.

Improve name resolution failure detection
4 participants