Rollup merge of #111560 - m-ou-se:span-char-boundary-stuff, r=cjgillot

Simplify find_width_of_character_at_span.

This makes `find_width_of_character_at_span` simpler and more robust against bad spans.

Fixes (but does not close, per beta policy) https://github.com/rust-lang/rust/issues/111485
This commit is contained in:
Matthias Krüger 2023-05-15 10:58:41 +02:00 committed by GitHub
commit ee914a4ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 24 deletions

View File

@ -20,6 +20,7 @@
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(round_char_boundary)]
#![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)] #![deny(rustc::diagnostic_outside_of_impl)]

View File

@ -1019,36 +1019,19 @@ fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
let src = local_begin.sf.external_src.borrow(); let src = local_begin.sf.external_src.borrow();
// We need to extend the snippet to the end of the src rather than to end_index so when let snippet = if let Some(src) = &local_begin.sf.src {
// searching forwards for boundaries we've got somewhere to search. src
let snippet = if let Some(ref src) = local_begin.sf.src {
&src[start_index..]
} else if let Some(src) = src.get_source() { } else if let Some(src) = src.get_source() {
&src[start_index..] src
} else { } else {
return 1; return 1;
}; };
debug!("snippet=`{:?}`", snippet);
let mut target = if forwards { end_index + 1 } else { end_index - 1 }; if forwards {
debug!("initial target=`{:?}`", target); (snippet.ceil_char_boundary(end_index + 1) - end_index) as u32
} else {
while !snippet.is_char_boundary(target - start_index) && target < source_len { (end_index - snippet.floor_char_boundary(end_index - 1)) as u32
target = if forwards {
target + 1
} else {
match target.checked_sub(1) {
Some(target) => target,
None => {
break;
}
}
};
debug!("target=`{:?}`", target);
} }
debug!("final target=`{:?}`", target);
if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
} }
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> { pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {