Skip to content

Commit

Permalink
Prevent suggestions from being emitted if all possible locations are …
Browse files Browse the repository at this point in the history
…inside expansions
  • Loading branch information
oli-obk authored and michaelwoerister committed Sep 29, 2017
1 parent 7c4e855 commit 99b9ffb
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
},
}
}
assert!(self.span.is_some(), "a file can't have no items and emit suggestions");
}
}

Expand Down Expand Up @@ -3568,8 +3567,7 @@ impl<'a> Resolver<'a> {
};
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();
}
Expand Down Expand Up @@ -3763,7 +3761,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<Span>,
candidates: &[ImportSuggestion],
better: bool,
found_use: bool) {
Expand All @@ -3781,18 +3780,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.
Expand Down

0 comments on commit 99b9ffb

Please sign in to comment.