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

Don't run everybody_loops for rustdoc; instead ignore resolution errors #73566

Merged
merged 24 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f6764c4
Initialize default providers only once
jyn514 Jul 15, 2020
a5275ff
Don't run everybody_loops for rustdoc
jyn514 Jun 20, 2020
b3187aa
Don't run analysis pass in rustdoc
jyn514 Jun 20, 2020
1b8accb
Add an option not to report resolution errors for rustdoc
jyn514 Jun 20, 2020
14a8707
Add `rustdoc` tests from #72088
ecstatic-morse Jun 7, 2020
768d6a4
Don't ICE on errors in function returning impl trait
jyn514 Jul 3, 2020
a93bcc9
Recurse into function bodies, but don't typeck closures
jyn514 Jul 9, 2020
d010443
Add test case for #65863
jyn514 Jul 9, 2020
cf844d2
Don't make typeck_tables_of public
jyn514 Jul 10, 2020
0cbc1cd
Avoid unnecessary enum
jyn514 Jul 10, 2020
3576f5d
Address review comments about code style
jyn514 Jul 10, 2020
bbe4971
Don't crash on Vec<DoesNotExist>
jyn514 Jul 10, 2020
2f29e69
Mention `cargo check` in help message
jyn514 Jul 10, 2020
763d373
Use tcx as the only context for visitor
jyn514 Jul 10, 2020
0759a55
Remove unnecessary lifetime parameter
jyn514 Jul 10, 2020
2d0e8e2
--bless
jyn514 Jul 10, 2020
02a24c8
Don't ICE on infinitely recursive types
jyn514 Jul 11, 2020
4c88070
Use mem::replace instead of rewriting it
jyn514 Jul 11, 2020
b2ff0e7
Fix comment
jyn514 Jul 11, 2020
ac9157b
EMPTY_MAP -> EMPTY_SET
jyn514 Jul 12, 2020
6eec9fb
Address review comments
jyn514 Jul 12, 2020
e117b47
Catch errors for any new item, not just trait implementations
jyn514 Jul 15, 2020
281ca13
Use the default providers in rustc_interface instead of adding our own
jyn514 Jul 15, 2020
631b2b9
Remove unused lazy_static
jyn514 Jul 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc_interface/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod queries;
pub mod util;

pub use interface::{run_compiler, Config};
pub use passes::{DEFAULT_EXTERN_QUERY_PROVIDERS, DEFAULT_QUERY_PROVIDERS};
pub use queries::Queries;

#[cfg(test)]
Expand Down
31 changes: 15 additions & 16 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::proc_macro_decls;
use crate::util;

use log::{info, log_enabled, warn};
use once_cell::sync::Lazy;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::{self, ast, visit};
use rustc_codegen_ssa::back::link::emit_metadata;
Expand All @@ -19,6 +20,7 @@ use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::middle;
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::steal::Steal;
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_mir as mir;
Expand Down Expand Up @@ -352,13 +354,7 @@ fn configure_and_expand_inner<'a>(
)
});

// If we're actually rustdoc then there's no need to actually compile
// anything, so switch everything to just looping
let mut should_loop = sess.opts.actually_rustdoc;
if let Some(PpMode::PpmSource(PpSourceMode::PpmEveryBodyLoops)) = sess.opts.pretty {
should_loop |= true;
}
if should_loop {
log::debug!("replacing bodies with loop {{}}");
util::ReplaceBodyWithLoop::new(&mut resolver).visit_crate(&mut krate);
}
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -719,7 +715,8 @@ pub fn prepare_outputs(
Ok(outputs)
}

pub fn default_provide(providers: &mut ty::query::Providers) {
pub static DEFAULT_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
let providers = &mut Providers::default();
providers.analysis = analysis;
proc_macro_decls::provide(providers);
plugin::build::provide(providers);
Expand All @@ -738,12 +735,15 @@ pub fn default_provide(providers: &mut ty::query::Providers) {
rustc_lint::provide(providers);
rustc_symbol_mangling::provide(providers);
rustc_codegen_ssa::provide(providers);
}
*providers
});

pub fn default_provide_extern(providers: &mut ty::query::Providers) {
rustc_metadata::provide_extern(providers);
rustc_codegen_ssa::provide_extern(providers);
}
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: Lazy<Providers> = Lazy::new(|| {
let mut extern_providers = *DEFAULT_QUERY_PROVIDERS;
rustc_metadata::provide_extern(&mut extern_providers);
rustc_codegen_ssa::provide_extern(&mut extern_providers);
extern_providers
});

pub struct QueryContext<'tcx>(&'tcx GlobalCtxt<'tcx>);

Expand Down Expand Up @@ -780,12 +780,11 @@ pub fn create_global_ctxt<'tcx>(
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

let codegen_backend = compiler.codegen_backend();
let mut local_providers = ty::query::Providers::default();
default_provide(&mut local_providers);
let mut local_providers = *DEFAULT_QUERY_PROVIDERS;
codegen_backend.provide(&mut local_providers);

let mut extern_providers = local_providers;
default_provide_extern(&mut extern_providers);
let mut extern_providers = *DEFAULT_EXTERN_QUERY_PROVIDERS;
codegen_backend.provide(&mut extern_providers);
codegen_backend.provide_extern(&mut extern_providers);

if let Some(callback) = compiler.override_queries {
Expand Down
99 changes: 74 additions & 25 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,23 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {

/// Fields used to add information to diagnostic errors.
diagnostic_metadata: DiagnosticMetadata<'ast>,

/// State used to know whether to ignore resolution errors for function bodies.
///
/// In particular, rustdoc uses this to avoid giving errors for `cfg()` items.
/// In most cases this will be `None`, in which case errors will always be reported.
/// If it is `Some(_)`, then it will be updated when entering a nested function or trait body.
in_func_body: bool,
}

/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
fn visit_item(&mut self, item: &'ast Item) {
let prev = replace(&mut self.diagnostic_metadata.current_item, Some(item));
// Always report errors in items we just entered.
let old_ignore = replace(&mut self.in_func_body, false);
self.resolve_item(item);
self.in_func_body = old_ignore;
self.diagnostic_metadata.current_item = prev;
}
fn visit_arm(&mut self, arm: &'ast Arm) {
Expand Down Expand Up @@ -497,13 +507,17 @@ impl<'a, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {

visit::walk_fn_ret_ty(this, &declaration.output);

// Ignore errors in function bodies if this is rustdoc
// Be sure not to set this until the function signature has been resolved.
let previous_state = replace(&mut this.in_func_body, true);
// Resolve the function body, potentially inside the body of an async closure
match fn_kind {
FnKind::Fn(.., body) => walk_list!(this, visit_block, body),
FnKind::Closure(_, body) => this.visit_expr(body),
};

debug!("(resolving function) leaving function");
this.in_func_body = previous_state;
})
});
self.diagnostic_metadata.current_function = previous_value;
Expand Down Expand Up @@ -644,6 +658,8 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
label_ribs: Vec::new(),
current_trait_ref: None,
diagnostic_metadata: DiagnosticMetadata::default(),
// errors at module scope should always be reported
in_func_body: false,
}
}

Expand Down Expand Up @@ -757,7 +773,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
return if self.is_label_valid_from_rib(i) {
Some(*id)
} else {
self.r.report_error(
self.report_error(
original_span,
ResolutionError::UnreachableLabel {
name: label.name,
Expand All @@ -775,7 +791,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
suggestion = suggestion.or_else(|| self.suggestion_for_label_in_rib(i, label));
}

self.r.report_error(
self.report_error(
original_span,
ResolutionError::UndeclaredLabel { name: label.name, suggestion },
);
Expand Down Expand Up @@ -833,7 +849,11 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
};
let report_error = |this: &Self, ns| {
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
this.r.session.span_err(ident.span, &format!("imports cannot refer to {}", what));
if this.should_report_errs() {
this.r
.session
.span_err(ident.span, &format!("imports cannot refer to {}", what));
}
};

for &ns in nss {
Expand Down Expand Up @@ -1008,7 +1028,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if seen_bindings.contains_key(&ident) {
let span = seen_bindings.get(&ident).unwrap();
let err = ResolutionError::NameAlreadyUsedInParameterList(ident.name, *span);
self.r.report_error(param.ident.span, err);
self.report_error(param.ident.span, err);
}
seen_bindings.entry(ident).or_insert(param.ident.span);

Expand Down Expand Up @@ -1274,7 +1294,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
.is_err()
{
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
self.r.report_error(span, err(ident.name, &path_names_to_string(path)));
self.report_error(span, err(ident.name, &path_names_to_string(path)));
}
}
}
Expand All @@ -1289,6 +1309,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}

fn resolve_local(&mut self, local: &'ast Local) {
debug!("resolving local ({:?})", local);
// Resolve the type.
walk_list!(self, visit_ty, &local.ty);

Expand Down Expand Up @@ -1390,7 +1411,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if inconsistent_vars.contains_key(name) {
v.could_be_path = false;
}
self.r.report_error(
self.report_error(
*v.origin.iter().next().unwrap(),
ResolutionError::VariableNotBoundInPattern(v),
);
Expand All @@ -1400,7 +1421,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
let mut inconsistent_vars = inconsistent_vars.iter().collect::<Vec<_>>();
inconsistent_vars.sort();
for (name, v) in inconsistent_vars {
self.r.report_error(v.0, ResolutionError::VariableBoundWithDifferentMode(*name, v.1));
self.report_error(v.0, ResolutionError::VariableBoundWithDifferentMode(*name, v.1));
}

// 5) Finally bubble up all the binding maps.
Expand Down Expand Up @@ -1550,7 +1571,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// `Variant(a, a)`:
_ => IdentifierBoundMoreThanOnceInSamePattern,
};
self.r.report_error(ident.span, error(ident.name));
self.report_error(ident.span, error(ident.name));
}

// Record as bound if it's valid:
Expand Down Expand Up @@ -1624,7 +1645,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// to something unusable as a pattern (e.g., constructor function),
// but we still conservatively report an error, see
// issues/33118#issuecomment-233962221 for one reason why.
self.r.report_error(
self.report_error(
ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable(
pat_src.descr(),
Expand Down Expand Up @@ -1677,18 +1698,27 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
source: PathSource<'ast>,
crate_lint: CrateLint,
) -> PartialRes {
log::debug!("smart_resolve_path_fragment(id={:?},qself={:?},path={:?}", id, qself, path);
let ns = source.namespace();
let is_expected = &|res| source.is_expected(res);

let report_errors = |this: &mut Self, res: Option<Res>| {
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);

let def_id = this.parent_scope.module.normal_ancestor_id;
let instead = res.is_some();
let suggestion =
if res.is_none() { this.report_missing_type_error(path) } else { None };

this.r.use_injections.push(UseError { err, candidates, def_id, instead, suggestion });
if this.should_report_errs() {
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);

let def_id = this.parent_scope.module.normal_ancestor_id;
let instead = res.is_some();
let suggestion =
if res.is_none() { this.report_missing_type_error(path) } else { None };

this.r.use_injections.push(UseError {
err,
candidates,
def_id,
instead,
suggestion,
});
}

PartialRes::new(Res::Err)
};
Expand Down Expand Up @@ -1746,13 +1776,17 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {

let def_id = this.parent_scope.module.normal_ancestor_id;

this.r.use_injections.push(UseError {
err,
candidates,
def_id,
instead: false,
suggestion: None,
});
if this.should_report_errs() {
this.r.use_injections.push(UseError {
err,
candidates,
def_id,
instead: false,
suggestion: None,
});
} else {
err.cancel();
}

// We don't return `Some(parent_err)` here, because the error will
// be already printed as part of the `use` injections
Expand Down Expand Up @@ -1809,7 +1843,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {

Err(err) => {
if let Some(err) = report_errors_for_call(self, err) {
self.r.report_error(err.span, err.node);
self.report_error(err.span, err.node);
}

PartialRes::new(Res::Err)
Expand Down Expand Up @@ -1843,6 +1877,21 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false }
}

/// A wrapper around [`Resolver::report_error`].
///
/// This doesn't emit errors for function bodies if this is rustdoc.
fn report_error(&self, span: Span, resolution_error: ResolutionError<'_>) {
if self.should_report_errs() {
self.r.report_error(span, resolution_error);
}
}

#[inline]
/// If we're actually rustdoc then avoid giving a name resolution error for `cfg()` items.
fn should_report_errs(&self) -> bool {
!(self.r.session.opts.actually_rustdoc && self.in_func_body)
}

// Resolve in alternative namespaces if resolution in the primary namespace fails.
fn resolve_qpath_anywhere(
&mut self,
Expand Down
Loading