Skip to content

Commit 3dea5ae

Browse files
committed
get the generic text and put it int he suggestion, but suggestion not working on derive subdiagnostic
1 parent 51dcf42 commit 3dea5ae

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ parser_expected_identifier = expected identifier
306306
parser_sugg_escape_to_use_as_identifier = escape `{$ident_name}` to use it as an identifier
307307
308308
parser_sugg_remove_comma = remove this comma
309-
parser_sugg_misplaced_generic = place the generic parameter list after the function name:
309+
parser_sugg_misplaced_generic = place the generic parameter list after the function name: `{$generic_as_string}`
310310
311311
parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
312312
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`

compiler/rustc_parse/src/errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,11 @@ pub(crate) struct SuggRemoveComma {
882882
pub span: Span,
883883
}
884884

885-
//SHREYS - should this be help, or a suggestion?
885+
//TODO: SHREYS - should this be help, or a suggestion?
886886
#[derive(Subdiagnostic)]
887-
#[help(parser::sugg_misplaced_generic)]
888887
pub(crate) struct SuggMisplacedGeneric {
888+
pub snippet: String,
889+
#[suggestion(code = "<{snippet}>")]
889890
#[primary_span]
890891
pub span: Span,
891892
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,21 @@ impl<'a> Parser<'a> {
330330
let token = self.token.clone();
331331
let suggest_misplaced_generic = if self.token == token::Lt {
332332
// store the span before the generic calculation starts
333-
if self.parse_generics().is_ok() {
334-
// FIXME: need the span of the whole generic.
335-
Some(SuggMisplacedGeneric { span: span.until(self.token.span) })
336-
} else {
337-
None
338-
}
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+
339348
} else {
340349
None
341350
};

0 commit comments

Comments
 (0)