Skip to content

Commit 8d15d4a

Browse files
committed
refactor away from derives and use span_suggestion() instead. Show's the correct(?) generic contents, but overwrites the fn name :(
1 parent 3dea5ae commit 8d15d4a

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

compiler/rustc_parse/src/errors.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ pub(crate) struct SuggRemoveComma {
883883
}
884884

885885
//TODO: SHREYS - should this be help, or a suggestion?
886-
#[derive(Subdiagnostic)]
887-
pub(crate) struct SuggMisplacedGeneric {
888-
pub snippet: String,
889-
#[suggestion(code = "<{snippet}>")]
890-
#[primary_span]
891-
pub span: Span,
892-
}
886+
// #[derive(Subdiagnostic)]
887+
// pub(crate) struct SuggMisplacedGeneric {
888+
// pub snippet: String,
889+
// #[suggestion(code = "<{snippet}>")]
890+
// #[primary_span]
891+
// pub span: Span,
892+
// }
893893

894894
#[derive(Subdiagnostic)]
895895
pub(crate) enum ExpectedIdentifierFound {
@@ -924,7 +924,6 @@ pub(crate) struct ExpectedIdentifier {
924924
pub token: Token,
925925
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
926926
pub suggest_remove_comma: Option<SuggRemoveComma>,
927-
pub suggest_misplaced_generic: Option<SuggMisplacedGeneric>,
928927
}
929928

930929
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
@@ -954,9 +953,6 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
954953
sugg.add_to_diagnostic(&mut diag);
955954
}
956955

957-
if let Some(sugg) = self.suggest_misplaced_generic {
958-
sugg.add_to_diagnostic(&mut diag);
959-
}
960956

961957
ExpectedIdentifierFound::new(token_descr, self.span).add_to_diagnostic(&mut diag);
962958

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::errors::{
1212
IncorrectAwait, IncorrectSemicolon, IncorrectUseOfAwait, ParenthesesInForHead,
1313
ParenthesesInForHeadSugg, PatternMethodParamWithoutBody, QuestionMarkInType,
1414
QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
15-
StructLiteralBodyWithoutPathSugg, SuggEscapeToUseAsIdentifier, SuggMisplacedGeneric,
15+
StructLiteralBodyWithoutPathSugg, SuggEscapeToUseAsIdentifier,
1616
SuggRemoveComma, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
1717
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
1818
};
@@ -326,38 +326,41 @@ impl<'a> Parser<'a> {
326326
None
327327
};
328328

329-
let span = self.token.span;
330-
let token = self.token.clone();
331-
let suggest_misplaced_generic = if self.token == token::Lt {
332-
// store the span before the generic calculation starts
333-
self.parse_generics().map_or(None, |generic| {
334-
// at this point, token should be Ident with the fn name
335-
336-
// FIXME: shreys the span should be the span of where the gneeric SHOULD go.
337-
// TODO: shreys what does this look like with invalid generic
338-
// TODO: shreys - what to do if no snippet? probably just HELP not suggestion
339-
340-
let snippet = match self.sess.source_map().span_to_snippet(generic.span) {
341-
Ok(snippet) => snippet,
342-
_ => "".to_string()
343-
};
344-
345-
Some(SuggMisplacedGeneric { span: self.token.span.to(generic.span.shrink_to_hi()), snippet })
346-
})
347-
348-
} else {
349-
None
350-
};
351-
352-
// FIXME: write better rust
329+
// FIXME: shreys write better rust
353330
let err = ExpectedIdentifier {
354-
span: if suggest_misplaced_generic.is_some() { span } else { self.token.span },
355-
token: if suggest_misplaced_generic.is_some() { token } else { self.token.clone() },
331+
span: self.token.span ,
332+
token: self.token.clone(),
356333
suggest_raw,
357334
suggest_remove_comma,
358-
suggest_misplaced_generic,
359335
};
360-
err.into_diagnostic(&self.sess.span_diagnostic)
336+
let mut err = err.into_diagnostic(&self.sess.span_diagnostic);
337+
338+
if self.token == token::Lt {
339+
// HACK: shreys shut the compiler up about unused result
340+
let _ = self.parse_generics()
341+
.map(|generic| {
342+
// at this point, token should be Ident with the fn name
343+
344+
// FIXME: shreys the span should be the span of where the gneeric SHOULD go.
345+
// TODO: shreys what does this look like with invalid generic
346+
// TODO: shreys - what to do if no snippet? probably just HELP not suggestion
347+
348+
let snippet = match self.sess.source_map().span_to_snippet(generic.span) {
349+
Ok(snippet) => snippet,
350+
_ => "".to_string()
351+
};
352+
353+
err.span_suggestion(
354+
self.token.span.to(generic.span.shrink_to_hi()),
355+
"help: place the generic parameter list after the function name:",
356+
snippet,
357+
Applicability::MachineApplicable
358+
);
359+
});
360+
}
361+
362+
err
363+
361364
}
362365

363366
pub(super) fn expected_one_of_not_found(

0 commit comments

Comments
 (0)