Auto merge of #103185 - chenyukang:yukang/fix-span-next-point, r=davi… · rust-lang/rust@53728ff (original) (raw)

`@@ -853,28 +853,36 @@ impl SourceMap {

`

853

853

`}

`

854

854

``

855

855

`/// Returns a new span representing the next character after the end-point of this span.

`

``

856

`+

/// Special cases:

`

``

857

`+

/// - if span is a dummy one, returns the same span

`

``

858

`+

/// - if next_point reached the end of source, return span with lo = hi

`

``

859

`+

/// - respect multi-byte characters

`

856

860

`pub fn next_point(&self, sp: Span) -> Span {

`

857

861

`if sp.is_dummy() {

`

858

862

`return sp;

`

859

863

`}

`

860

864

`let start_of_next_point = sp.hi().0;

`

861

865

``

862

``

`-

let width = self.find_width_of_character_at_span(sp.shrink_to_hi(), true);

`

863

``

`` -

// If the width is 1, then the next span should point to the same lo and hi. However,

``

864

``

`-

// in the case of a multibyte character, where the width != 1, the next span should

`

``

866

`+

let width = self.find_width_of_character_at_span(sp, true);

`

``

867

`+

if width == 0 {

`

``

868

`+

return Span::new(sp.hi(), sp.hi(), sp.ctxt(), None);

`

``

869

`+

}

`

``

870

`+

// If the width is 1, then the next span should only contain the next char besides current ending.

`

``

871

`+

// However, in the case of a multibyte character, where the width != 1, the next span should

`

865

872

`// span multiple bytes to include the whole character.

`

866

873

`let end_of_next_point =

`

867

``

`-

start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);

`

``

874

`+

start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);

`

868

875

``

869

``

`-

let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));

`

``

876

`+

let end_of_next_point = BytePos(cmp::max(start_of_next_point + 1, end_of_next_point));

`

870

877

`Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)

`

871

878

`}

`

872

879

``

873

880

`/// Finds the width of the character, either before or after the end of provided span,

`

874

881

`` /// depending on the forwards parameter.

``

875

882

`fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {

`

876

883

`let sp = sp.data();

`

877

``

`-

if sp.lo == sp.hi {

`

``

884

+

``

885

`+

if sp.lo == sp.hi && !forwards {

`

878

886

`debug!("find_width_of_character_at_span: early return empty span");

`

879

887

`return 1;

`

880

888

`}

`

`@@ -908,9 +916,9 @@ impl SourceMap {

`

908

916

`let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();

`

909

917

`` debug!("find_width_of_character_at_span: source_len={:?}", source_len);

``

910

918

`// Ensure indexes are also not malformed.

`

911

``

`-

if start_index > end_index || end_index > source_len {

`

``

919

`+

if start_index > end_index || end_index > source_len - 1 {

`

912

920

`debug!("find_width_of_character_at_span: source indexes are malformed");

`

913

``

`-

return 1;

`

``

921

`+

return 0;

`

914

922

`}

`

915

923

``

916

924

`let src = local_begin.sf.external_src.borrow();

`