Skip to content

Commit f0e7a5b

Browse files
committed
Prevent suggestions from being emitted if all possible locations are inside expansions
1 parent 74748b1 commit f0e7a5b

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/librustc_resolve/lib.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
631631
},
632632
}
633633
}
634-
assert!(self.span.is_some(), "a file can't have no items and emit suggestions");
635634
}
636635
}
637636

@@ -3562,8 +3561,7 @@ impl<'a> Resolver<'a> {
35623561
};
35633562
visit::walk_crate(&mut finder, krate);
35643563
if !candidates.is_empty() {
3565-
let span = finder.span.expect("did not find module");
3566-
show_candidates(&mut err, span, &candidates, better, finder.found_use);
3564+
show_candidates(&mut err, finder.span, &candidates, better, finder.found_use);
35673565
}
35683566
err.emit();
35693567
}
@@ -3757,7 +3755,8 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St
37573755
/// entities with that name in all crates. This method allows outputting the
37583756
/// results of this search in a programmer-friendly way
37593757
fn show_candidates(err: &mut DiagnosticBuilder,
3760-
span: Span,
3758+
// This is `None` if all placement locations are inside expansions
3759+
span: Option<Span>,
37613760
candidates: &[ImportSuggestion],
37623761
better: bool,
37633762
found_use: bool) {
@@ -3775,18 +3774,27 @@ fn show_candidates(err: &mut DiagnosticBuilder,
37753774
};
37763775
let msg = format!("possible {}candidate{} into scope", better, msg_diff);
37773776

3778-
for candidate in &mut path_strings {
3779-
// produce an additional newline to separate the new use statement
3780-
// from the directly following item.
3781-
let additional_newline = if found_use {
3782-
""
3783-
} else {
3784-
"\n"
3785-
};
3786-
*candidate = format!("use {};\n{}", candidate, additional_newline);
3787-
}
3777+
if let Some(span) = span {
3778+
for candidate in &mut path_strings {
3779+
// produce an additional newline to separate the new use statement
3780+
// from the directly following item.
3781+
let additional_newline = if found_use {
3782+
""
3783+
} else {
3784+
"\n"
3785+
};
3786+
*candidate = format!("use {};\n{}", candidate, additional_newline);
3787+
}
37883788

3789-
err.span_suggestions(span, &msg, path_strings);
3789+
err.span_suggestions(span, &msg, path_strings);
3790+
} else {
3791+
let mut msg = msg;
3792+
msg.push(':');
3793+
for candidate in path_strings {
3794+
msg.push('\n');
3795+
msg.push_str(&candidate);
3796+
}
3797+
}
37903798
}
37913799

37923800
/// A somewhat inefficient routine to obtain the name of a module.

0 commit comments

Comments
 (0)