Skip to content

Commit ec24833

Browse files
authored
Rollup merge of rust-lang#68845 - dwrensha:fix-68783, r=estebank
stop using BytePos for computing spans in librustc_parse/parser/mod.rs Computing spans using logic such as `self.token.span.lo() + BytePos(1)` can cause internal compiler errors like rust-lang#68730 when non-ascii characters are given as input. rust-lang#68735 partially addressed this problem, but only for one case. Moreover, its usage of `next_point()` does not actually align with what `bump_with()` expects. For example, given the token `>>=`, we should pass the span consisting of the final two characters `>=`, but `next_point()` advances the span beyond the end of the `=`. This pull request instead computes the start of the new span by doing `start_point(self.token.span).hi()`. This matches `self.token.span.lo() + BytePos(1)` in the common case where the characters are ascii, and it gracefully handles multibyte characters. Fixes rust-lang#68783.
2 parents 16e4e8f + 9ac68e1 commit ec24833

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/librustc_parse/parser/mod.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, FatalError
2121
use rustc_session::parse::ParseSess;
2222
use rustc_span::source_map::respan;
2323
use rustc_span::symbol::{kw, sym, Symbol};
24-
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
24+
use rustc_span::{FileName, Span, DUMMY_SP};
2525
use syntax::ast::{self, AttrStyle, AttrVec, CrateSugar, Extern, Ident, Unsafety, DUMMY_NODE_ID};
2626
use syntax::ast::{IsAsync, MacArgs, MacDelimiter, Mutability, StrLit, Visibility, VisibilityKind};
2727
use syntax::ptr::P;
@@ -615,8 +615,8 @@ impl<'a> Parser<'a> {
615615
true
616616
}
617617
token::BinOpEq(token::Plus) => {
618-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
619-
self.bump_with(token::Eq, span);
618+
let start_point = self.sess.source_map().start_point(self.token.span);
619+
self.bump_with(token::Eq, self.token.span.with_lo(start_point.hi()));
620620
true
621621
}
622622
_ => false,
@@ -633,8 +633,9 @@ impl<'a> Parser<'a> {
633633
Ok(())
634634
}
635635
token::AndAnd => {
636-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
637-
Ok(self.bump_with(token::BinOp(token::And), span))
636+
let start_point = self.sess.source_map().start_point(self.token.span);
637+
Ok(self
638+
.bump_with(token::BinOp(token::And), self.token.span.with_lo(start_point.hi())))
638639
}
639640
_ => self.unexpected(),
640641
}
@@ -650,8 +651,9 @@ impl<'a> Parser<'a> {
650651
Ok(())
651652
}
652653
token::OrOr => {
653-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
654-
Ok(self.bump_with(token::BinOp(token::Or), span))
654+
let start_point = self.sess.source_map().start_point(self.token.span);
655+
Ok(self
656+
.bump_with(token::BinOp(token::Or), self.token.span.with_lo(start_point.hi())))
655657
}
656658
_ => self.unexpected(),
657659
}
@@ -671,13 +673,16 @@ impl<'a> Parser<'a> {
671673
true
672674
}
673675
token::BinOp(token::Shl) => {
674-
let span = self.sess.source_map().next_point(self.token.span);
675-
self.bump_with(token::Lt, span);
676+
let start_point = self.sess.source_map().start_point(self.token.span);
677+
self.bump_with(token::Lt, self.token.span.with_lo(start_point.hi()));
676678
true
677679
}
678680
token::LArrow => {
679-
let span = self.sess.source_map().next_point(self.token.span);
680-
self.bump_with(token::BinOp(token::Minus), span);
681+
let start_point = self.sess.source_map().start_point(self.token.span);
682+
self.bump_with(
683+
token::BinOp(token::Minus),
684+
self.token.span.with_lo(start_point.hi()),
685+
);
681686
true
682687
}
683688
_ => false,
@@ -707,16 +712,16 @@ impl<'a> Parser<'a> {
707712
Some(())
708713
}
709714
token::BinOp(token::Shr) => {
710-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
711-
Some(self.bump_with(token::Gt, span))
715+
let start_point = self.sess.source_map().start_point(self.token.span);
716+
Some(self.bump_with(token::Gt, self.token.span.with_lo(start_point.hi())))
712717
}
713718
token::BinOpEq(token::Shr) => {
714-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
715-
Some(self.bump_with(token::Ge, span))
719+
let start_point = self.sess.source_map().start_point(self.token.span);
720+
Some(self.bump_with(token::Ge, self.token.span.with_lo(start_point.hi())))
716721
}
717722
token::Ge => {
718-
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
719-
Some(self.bump_with(token::Eq, span))
723+
let start_point = self.sess.source_map().start_point(self.token.span);
724+
Some(self.bump_with(token::Eq, self.token.span.with_lo(start_point.hi())))
720725
}
721726
_ => None,
722727
};

src/test/ui/parser/issue-68730.stderr

-1 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)