From f0e7a5b8e51ea7e2e6b66293d96696d68491b6ad Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 1 Sep 2017 11:14:04 +0200 Subject: [PATCH] Prevent suggestions from being emitted if all possible locations are inside expansions --- src/librustc_resolve/lib.rs | 38 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 88e092a1684..d23c7f199a5 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -631,7 +631,6 @@ fn visit_mod( }, } } - assert!(self.span.is_some(), "a file can't have no items and emit suggestions"); } } @@ -3562,8 +3561,7 @@ fn report_with_use_injections(&mut self, krate: &Crate) { }; visit::walk_crate(&mut finder, krate); if !candidates.is_empty() { - let span = finder.span.expect("did not find module"); - show_candidates(&mut err, span, &candidates, better, finder.found_use); + show_candidates(&mut err, finder.span, &candidates, better, finder.found_use); } err.emit(); } @@ -3757,7 +3755,8 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St /// entities with that name in all crates. This method allows outputting the /// results of this search in a programmer-friendly way fn show_candidates(err: &mut DiagnosticBuilder, - span: Span, + // This is `None` if all placement locations are inside expansions + span: Option, candidates: &[ImportSuggestion], better: bool, found_use: bool) { @@ -3775,18 +3774,27 @@ fn show_candidates(err: &mut DiagnosticBuilder, }; let msg = format!("possible {}candidate{} into scope", better, msg_diff); - for candidate in &mut path_strings { - // produce an additional newline to separate the new use statement - // from the directly following item. - let additional_newline = if found_use { - "" - } else { - "\n" - }; - *candidate = format!("use {};\n{}", candidate, additional_newline); - } + if let Some(span) = span { + for candidate in &mut path_strings { + // produce an additional newline to separate the new use statement + // from the directly following item. + let additional_newline = if found_use { + "" + } else { + "\n" + }; + *candidate = format!("use {};\n{}", candidate, additional_newline); + } - err.span_suggestions(span, &msg, path_strings); + err.span_suggestions(span, &msg, path_strings); + } else { + let mut msg = msg; + msg.push(':'); + for candidate in path_strings { + msg.push('\n'); + msg.push_str(&candidate); + } + } } /// A somewhat inefficient routine to obtain the name of a module.