Skip to content

Commit 02f57f8

Browse files
committed
review comments
1 parent f1499a8 commit 02f57f8

File tree

7 files changed

+81
-41
lines changed

7 files changed

+81
-41
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,13 @@ impl Diagnostic {
298298
/// * may contain a name of a function, variable, or type, but not whole expressions
299299
///
300300
/// See `CodeSuggestion` for more information.
301-
pub fn span_suggestion(&mut self, sp: Span, msg: &str,
302-
suggestion: String,
303-
applicability: Applicability) -> &mut Self {
301+
pub fn span_suggestion(
302+
&mut self,
303+
sp: Span,
304+
msg: &str,
305+
suggestion: String,
306+
applicability: Applicability,
307+
) -> &mut Self {
304308
self.suggestions.push(CodeSuggestion {
305309
substitutions: vec![Substitution {
306310
parts: vec![SubstitutionPart {
@@ -315,10 +319,35 @@ impl Diagnostic {
315319
self
316320
}
317321

322+
pub fn span_suggestion_verbose(
323+
&mut self,
324+
sp: Span,
325+
msg: &str,
326+
suggestion: String,
327+
applicability: Applicability,
328+
) -> &mut Self {
329+
self.suggestions.push(CodeSuggestion {
330+
substitutions: vec![Substitution {
331+
parts: vec![SubstitutionPart {
332+
snippet: suggestion,
333+
span: sp,
334+
}],
335+
}],
336+
msg: msg.to_owned(),
337+
style: SuggestionStyle::ShowAlways,
338+
applicability,
339+
});
340+
self
341+
}
342+
318343
/// Prints out a message with multiple suggested edits of the code.
319-
pub fn span_suggestions(&mut self, sp: Span, msg: &str,
320-
suggestions: impl Iterator<Item = String>, applicability: Applicability) -> &mut Self
321-
{
344+
pub fn span_suggestions(
345+
&mut self,
346+
sp: Span,
347+
msg: &str,
348+
suggestions: impl Iterator<Item = String>,
349+
applicability: Applicability,
350+
) -> &mut Self {
322351
self.suggestions.push(CodeSuggestion {
323352
substitutions: suggestions.map(|snippet| Substitution {
324353
parts: vec![SubstitutionPart {

src/librustc_errors/emitter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ pub trait Emitter {
221221
// when this style is set we want the suggestion to be a message, not inline
222222
sugg.style != SuggestionStyle::HideCodeAlways &&
223223
// trivial suggestion for tooling's sake, never shown
224-
sugg.style != SuggestionStyle::CompletelyHidden
224+
sugg.style != SuggestionStyle::CompletelyHidden &&
225+
// subtle suggestion, never shown inline
226+
sugg.style != SuggestionStyle::ShowAlways
225227
{
226228
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
227229
let msg = if substitution.len() == 0 || sugg.style.hide_inline() {

src/librustc_errors/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub enum SuggestionStyle {
8181
/// This will *not* show the code if the suggestion is inline *and* the suggested code is
8282
/// empty.
8383
ShowCode,
84+
/// Always show the suggested code independently.
85+
ShowAlways,
8486
}
8587

8688
impl SuggestionStyle {

src/libsyntax/parse/diagnostics.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan, SpanSnippetError};
1717
use log::{debug, trace};
1818
use std::mem;
1919

20-
const TURBOFISH: &'static str = "use the \"turbofish\" `::<...>` instead of `<...>` to specify \
21-
type arguments";
20+
const TURBOFISH: &'static str = "use `::<...>` instead of `<...>` to specify type arguments";
2221
/// Creates a placeholder argument.
2322
crate fn dummy_arg(ident: Ident) -> Param {
2423
let pat = P(Pat {
@@ -585,7 +584,7 @@ impl<'a> Parser<'a> {
585584
);
586585

587586
let suggest = |err: &mut DiagnosticBuilder<'_>| {
588-
err.span_suggestion(
587+
err.span_suggestion_verbose(
589588
op_span.shrink_to_lo(),
590589
TURBOFISH,
591590
"::".to_string(),
@@ -647,29 +646,16 @@ impl<'a> Parser<'a> {
647646
// We have high certainty that this was a bad turbofish at this point.
648647
// `foo< bar >(`
649648
suggest(&mut err);
650-
651-
let snapshot = self.clone();
652-
self.bump(); // `(`
653-
654649
// Consume the fn call arguments.
655-
let modifiers = [
656-
(token::OpenDelim(token::Paren), 1),
657-
(token::CloseDelim(token::Paren), -1),
658-
];
659-
self.consume_tts(1, &modifiers[..]);
660-
661-
if self.token.kind == token::Eof {
662-
// Not entirely sure now, but we bubble the error up with the
663-
// suggestion.
664-
mem::replace(self, snapshot);
665-
Err(err)
666-
} else {
667-
// 99% certain that the suggestion is correct, continue parsing.
668-
err.emit();
669-
// FIXME: actually check that the two expressions in the binop are
670-
// paths and resynthesize new fn call expression instead of using
671-
// `ExprKind::Err` placeholder.
672-
mk_err_expr(self, lhs.span.to(self.prev_span))
650+
match self.consume_fn_args() {
651+
Err(()) => Err(err),
652+
Ok(()) => {
653+
err.emit();
654+
// FIXME: actually check that the two expressions in the binop are
655+
// paths and resynthesize new fn call expression instead of using
656+
// `ExprKind::Err` placeholder.
657+
mk_err_expr(self, lhs.span.to(self.prev_span))
658+
}
673659
}
674660
} else {
675661
// All we know is that this is `foo < bar >` and *nothing* else. Try to
@@ -687,6 +673,27 @@ impl<'a> Parser<'a> {
687673
Ok(None)
688674
}
689675

676+
fn consume_fn_args(&mut self) -> Result<(), ()> {
677+
let snapshot = self.clone();
678+
self.bump(); // `(`
679+
680+
// Consume the fn call arguments.
681+
let modifiers = [
682+
(token::OpenDelim(token::Paren), 1),
683+
(token::CloseDelim(token::Paren), -1),
684+
];
685+
self.consume_tts(1, &modifiers[..]);
686+
687+
if self.token.kind == token::Eof {
688+
// Not entirely sure that what we consumed were fn arguments, rollback.
689+
mem::replace(self, snapshot);
690+
Err(())
691+
} else {
692+
// 99% certain that the suggestion is correct, continue parsing.
693+
Ok(())
694+
}
695+
}
696+
690697
crate fn maybe_report_ambiguous_plus(
691698
&mut self,
692699
allow_plus: bool,

src/test/ui/did_you_mean/issue-40396.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ error: chained comparison operators require parentheses
33
|
44
LL | (0..13).collect<Vec<i32>>();
55
| ^^^^^
6-
help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
6+
help: use `::<...>` instead of `<...>` to specify type arguments
77
|
88
LL | (0..13).collect::<Vec<i32>>();
99
| ^^
@@ -13,7 +13,7 @@ error: chained comparison operators require parentheses
1313
|
1414
LL | Vec<i32>::new();
1515
| ^^^^^
16-
help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
16+
help: use `::<...>` instead of `<...>` to specify type arguments
1717
|
1818
LL | Vec::<i32>::new();
1919
| ^^
@@ -23,7 +23,7 @@ error: chained comparison operators require parentheses
2323
|
2424
LL | (0..13).collect<Vec<i32>();
2525
| ^^^^^
26-
help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
26+
help: use `::<...>` instead of `<...>` to specify type arguments
2727
|
2828
LL | (0..13).collect::<Vec<i32>();
2929
| ^^

src/test/ui/parser/require-parens-for-chained-comparison.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ fn main() {
1212

1313
f<X>();
1414
//~^ ERROR chained comparison operators require parentheses
15-
//~| HELP use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
15+
//~| HELP use `::<...>` instead of `<...>` to specify type arguments
1616

1717
f<Result<Option<X>, Option<Option<X>>>(1, 2);
1818
//~^ ERROR chained comparison operators require parentheses
19-
//~| HELP use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
19+
//~| HELP use `::<...>` instead of `<...>` to specify type arguments
2020

2121
use std::convert::identity;
2222
let _ = identity<u8>;
2323
//~^ ERROR chained comparison operators require parentheses
24-
//~| HELP use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
24+
//~| HELP use `::<...>` instead of `<...>` to specify type arguments
2525
//~| HELP or use `(...)` if you meant to specify fn arguments
2626
}

src/test/ui/parser/require-parens-for-chained-comparison.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error: chained comparison operators require parentheses
1515
|
1616
LL | f<X>();
1717
| ^^^
18-
help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
18+
help: use `::<...>` instead of `<...>` to specify type arguments
1919
|
2020
LL | f::<X>();
2121
| ^^
@@ -25,7 +25,7 @@ error: chained comparison operators require parentheses
2525
|
2626
LL | f<Result<Option<X>, Option<Option<X>>>(1, 2);
2727
| ^^^^^^^^
28-
help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
28+
help: use `::<...>` instead of `<...>` to specify type arguments
2929
|
3030
LL | f::<Result<Option<X>, Option<Option<X>>>(1, 2);
3131
| ^^
@@ -36,7 +36,7 @@ error: chained comparison operators require parentheses
3636
LL | let _ = identity<u8>;
3737
| ^^^^
3838
|
39-
= help: use the "turbofish" `::<...>` instead of `<...>` to specify type arguments
39+
= help: use `::<...>` instead of `<...>` to specify type arguments
4040
= help: or use `(...)` if you meant to specify fn arguments
4141

4242
error[E0308]: mismatched types

0 commit comments

Comments
 (0)