Skip to content

Commit c10e769

Browse files
committed
--wip-- [skip ci]
1 parent 31d754a commit c10e769

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +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:
309310
310311
parser_expected_semi_found_reserved_identifier_str = expected `;`, found reserved identifier `{$token}`
311312
parser_expected_semi_found_keyword_str = expected `;`, found keyword `{$token}`

compiler/rustc_interface/src/queries.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl<'tcx> Queries<'tcx> {
121121
}
122122

123123
pub fn parse(&self) -> Result<&Query<ast::Crate>> {
124+
debug!("HELLO THERE");
124125
self.parse.compute(|| {
125126
passes::parse(self.session(), &self.compiler.input)
126127
.map_err(|mut parse_error| parse_error.emit())

compiler/rustc_parse/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,15 @@ pub(crate) struct SuggRemoveComma {
882882
pub span: Span,
883883
}
884884

885+
//SHREYS - should this be help, or a suggestion?
886+
#[derive(Subdiagnostic)]
887+
#[help(parser::sugg_misplaced_generic)]
888+
pub(crate) struct SuggMisplacedGeneric {
889+
#[primary_span]
890+
pub span: Span,
891+
}
892+
893+
885894
#[derive(Subdiagnostic)]
886895
pub(crate) enum ExpectedIdentifierFound {
887896
#[label(parser_expected_identifier_found_reserved_identifier)]
@@ -915,6 +924,7 @@ pub(crate) struct ExpectedIdentifier {
915924
pub token: Token,
916925
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
917926
pub suggest_remove_comma: Option<SuggRemoveComma>,
927+
pub suggest_misplaced_generic: Option<SuggMisplacedGeneric>,
918928
}
919929

920930
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
@@ -944,6 +954,10 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for ExpectedIdentifier {
944954
sugg.add_to_diagnostic(&mut diag);
945955
}
946956

957+
if let Some(sugg) = self.suggest_misplaced_generic {
958+
sugg.add_to_diagnostic(&mut diag);
959+
}
960+
947961
ExpectedIdentifierFound::new(token_descr, self.span).add_to_diagnostic(&mut diag);
948962

949963
if let Some(sugg) = self.suggest_remove_comma {

compiler/rustc_parse/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ macro_rules! panictry_buffer {
5656
}
5757
}};
5858
}
59-
6059
pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> {
6160
let mut parser = new_parser_from_file(sess, input, None);
6261
parser.parse_crate_mod()

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::errors::{
1414
QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
1515
StructLiteralBodyWithoutPathSugg, SuggEscapeToUseAsIdentifier, SuggRemoveComma,
1616
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
17-
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
17+
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, SuggMisplacedGeneric,
1818
};
1919

2020
use crate::lexer::UnmatchedBrace;
@@ -291,7 +291,8 @@ impl<'a> Parser<'a> {
291291
self.sess.source_map().span_to_snippet(span)
292292
}
293293

294-
pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
294+
#[instrument(level = "debug", skip(self))]
295+
pub(super) fn expected_ident_found(&mut self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
295296
let valid_follow = &[
296297
TokenKind::Eq,
297298
TokenKind::Colon,
@@ -325,11 +326,40 @@ impl<'a> Parser<'a> {
325326
None
326327
};
327328

329+
let span = self.token.span;
330+
let token = self.token.clone();
331+
let suggest_misplaced_generic =
332+
if self.token == token::Lt {
333+
// store the span before the generic calculation starts
334+
if self.parse_generics().is_ok() {
335+
// FIXME: need the span of the whole generic.
336+
Some(
337+
SuggMisplacedGeneric {
338+
span: span.until(self.token.span)
339+
}
340+
)
341+
} else {
342+
None
343+
}
344+
} else {
345+
None
346+
};
347+
348+
// FIXME: write better rust
328349
let err = ExpectedIdentifier {
329-
span: self.token.span,
330-
token: self.token.clone(),
350+
span: if suggest_misplaced_generic.is_some() {
351+
span
352+
} else {
353+
self.token.span
354+
},
355+
token: if suggest_misplaced_generic.is_some() {
356+
token
357+
} else {
358+
self.token.clone()
359+
},
331360
suggest_raw,
332361
suggest_remove_comma,
362+
suggest_misplaced_generic,
333363
};
334364
err.into_diagnostic(&self.sess.span_diagnostic)
335365
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ impl<'a> Parser<'a> {
5959

6060
let post_attr_lo = self.token.span;
6161
let mut items = vec![];
62+
debug!("fails to parse an item!");
6263
while let Some(item) = self.parse_item(ForceCollect::No)? {
6364
items.push(item);
6465
self.maybe_consume_incorrect_semicolon(&items);
6566
}
66-
67+
debug!("Does not make it here!");
6768
if !self.eat(term) {
6869
let token_str = super::token_descr(&self.token);
6970
if !self.maybe_consume_incorrect_semicolon(&items) {
@@ -110,6 +111,7 @@ impl<'a> Parser<'a> {
110111
fn_parse_mode: FnParseMode,
111112
force_collect: ForceCollect,
112113
) -> PResult<'a, Option<Item>> {
114+
debug!("fn parse_item_common");
113115
// Don't use `maybe_whole` so that we have precise control
114116
// over when we bump the parser
115117
if let token::Interpolated(nt) = &self.token.kind && let token::NtItem(item) = &**nt {
@@ -125,10 +127,10 @@ impl<'a> Parser<'a> {
125127
self.collect_tokens_trailing_token(attrs, force_collect, |this: &mut Self, attrs| {
126128
let item =
127129
this.parse_item_common_(attrs, mac_allowed, attrs_allowed, fn_parse_mode);
130+
debug!(?item);
128131
unclosed_delims.append(&mut this.unclosed_delims);
129132
Ok((item?, TrailingToken::None))
130133
})?;
131-
132134
self.unclosed_delims.append(&mut unclosed_delims);
133135
Ok(item)
134136
}
@@ -213,6 +215,7 @@ impl<'a> Parser<'a> {
213215
self.parse_use_item()?
214216
} else if self.check_fn_front_matter(def_final) {
215217
// FUNCTION ITEM
218+
debug!("parse_item_kind: inside fn item");
216219
let (ident, sig, generics, body) = self.parse_fn(attrs, fn_parse_mode, lo, vis)?;
217220
(ident, ItemKind::Fn(Box::new(Fn { defaultness: def(), sig, generics, body })))
218221
} else if self.eat_keyword(kw::Extern) {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,10 @@ impl<'a> Parser<'a> {
540540
self.parse_ident_common(true)
541541
}
542542

543-
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
543+
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
544+
debug!("fn ident_or_err");
545+
debug!(?self.token);
546+
debug!(prev = ?self.prev_token);
544547
self.token.ident().ok_or_else(|| match self.prev_token.kind {
545548
TokenKind::DocComment(..) => DocCommentDoesNotDocumentAnything {
546549
span: self.prev_token.span,
@@ -550,7 +553,8 @@ impl<'a> Parser<'a> {
550553
_ => self.expected_ident_found(),
551554
})
552555
}
553-
556+
557+
// SHREYS - exits after ident_or_err()?
554558
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
555559
let (ident, is_raw) = self.ident_or_err()?;
556560
if !is_raw && ident.is_reserved() {

0 commit comments

Comments
 (0)