Skip to content

Commit

Permalink
Rollup merge of #108353 - petrochenkov:rmir, r=cjgillot
Browse files Browse the repository at this point in the history
resolve: Remove `ImportResolver`

It's a trivial wrapper over `Resolver` that doesn't bring any benefits
  • Loading branch information
matthiaskrgr authored Feb 22, 2023
2 parents baf6a72 + d275114 commit 9f5c401
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 103 deletions.
40 changes: 19 additions & 21 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_span::{BytePos, Span, SyntaxContext};
use thin_vec::ThinVec;

use crate::errors as errs;
use crate::imports::{Import, ImportKind, ImportResolver};
use crate::imports::{Import, ImportKind};
use crate::late::{PatternSource, Rib};
use crate::path_names_to_string;
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
Expand Down Expand Up @@ -1888,15 +1888,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
(format!("use of undeclared crate or module `{}`", ident), suggestion)
}
}
}

impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
/// Adds suggestions for a path that cannot be resolved.
pub(crate) fn make_path_suggestion(
&mut self,
span: Span,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
parent_scope: &ParentScope<'a>,
) -> Option<(Vec<Segment>, Option<String>)> {
debug!("make_path_suggestion: span={:?} path={:?}", span, path);

Expand Down Expand Up @@ -1931,11 +1929,11 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
fn make_missing_self_suggestion(
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
parent_scope: &ParentScope<'a>,
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `self` and check if that is valid.
path[0].ident.name = kw::SelfLower;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
let result = self.maybe_resolve_path(&path, None, parent_scope);
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
}
Expand All @@ -1950,11 +1948,11 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
fn make_missing_crate_suggestion(
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
parent_scope: &ParentScope<'a>,
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Crate;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
let result = self.maybe_resolve_path(&path, None, parent_scope);
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
if let PathResult::Module(..) = result {
Some((
Expand All @@ -1981,11 +1979,11 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
fn make_missing_super_suggestion(
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
parent_scope: &ParentScope<'a>,
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Super;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
let result = self.maybe_resolve_path(&path, None, parent_scope);
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
}
Expand All @@ -2003,7 +2001,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
fn make_external_crate_suggestion(
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
parent_scope: &ParentScope<'a>,
) -> Option<(Vec<Segment>, Option<String>)> {
if path[1].ident.span.is_rust_2015() {
return None;
Expand All @@ -2013,13 +2011,13 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
// 1) some consistent ordering for emitted diagnostics, and
// 2) `std` suggestions before `core` suggestions.
let mut extern_crate_names =
self.r.extern_prelude.iter().map(|(ident, _)| ident.name).collect::<Vec<_>>();
self.extern_prelude.iter().map(|(ident, _)| ident.name).collect::<Vec<_>>();
extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());

for name in extern_crate_names.into_iter() {
// Replace first ident with a crate name and check if that is valid.
path[0].ident.name = name;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
let result = self.maybe_resolve_path(&path, None, parent_scope);
debug!(
"make_external_crate_suggestion: name={:?} path={:?} result={:?}",
name, path, result
Expand All @@ -2046,8 +2044,8 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
/// ```
pub(crate) fn check_for_module_export_macro(
&mut self,
import: &'b Import<'b>,
module: ModuleOrUniformRoot<'b>,
import: &'a Import<'a>,
module: ModuleOrUniformRoot<'a>,
ident: Ident,
) -> Option<(Option<Suggestion>, Option<String>)> {
let ModuleOrUniformRoot::Module(mut crate_module) = module else {
Expand All @@ -2064,8 +2062,8 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
return None;
}

let resolutions = self.r.resolutions(crate_module).borrow();
let resolution = resolutions.get(&self.r.new_key(ident, MacroNS))?;
let resolutions = self.resolutions(crate_module).borrow();
let resolution = resolutions.get(&self.new_key(ident, MacroNS))?;
let binding = resolution.borrow().binding()?;
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
let module_name = crate_module.kind.name().unwrap();
Expand All @@ -2086,7 +2084,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
// ie. `use a::b::{c, d, e};`
// ^^^
let (found_closing_brace, binding_span) = find_span_of_binding_until_next_binding(
self.r.tcx.sess,
self.tcx.sess,
import.span,
import.use_span,
);
Expand All @@ -2105,7 +2103,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
// ie. `use a::b::{c, d};`
// ^^^
if let Some(previous_span) =
extend_span_to_previous_binding(self.r.tcx.sess, binding_span)
extend_span_to_previous_binding(self.tcx.sess, binding_span)
{
debug!("check_for_module_export_macro: previous_span={:?}", previous_span);
removal_span = removal_span.with_lo(previous_span.lo());
Expand All @@ -2123,7 +2121,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
// or `use a::{b, c, d}};`
// ^^^^^^^^^^^
let (has_nested, after_crate_name) = find_span_immediately_after_crate_name(
self.r.tcx.sess,
self.tcx.sess,
module_name,
import.use_span,
);
Expand All @@ -2132,7 +2130,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
has_nested, after_crate_name
);

let source_map = self.r.tcx.sess.source_map();
let source_map = self.tcx.sess.source_map();

// Make sure this is actually crate-relative.
let is_definitely_crate = import
Expand Down
Loading

0 comments on commit 9f5c401

Please sign in to comment.