Skip to content

Commit 26faee0

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40092 - sinkuu:fix_suggestion_index, r=pnkfelix
Fix suggestion span error with a line containing multibyte characters This PR fixes broken suggestions caused by multibyte characters. e.g. for this code, rustc provides a broken suggestion ([playground](https://is.gd/DWGLu7)): ```rust fn main() { let tup = (1,); println!("☃{}", tup[0]); } ``` ``` error: cannot index a value of type `({integer},)` --> <anon>:3:21 | 3 | println!("☃{}", tup[0]); | ^^^^^^ | help: to access tuple elements, use tuple indexing syntax as shown | println!("☃{}"tup.00]); error: aborting due to previous error ``` `CodeSuggestion::splice_lines` is misusing `Loc.col` (`CharPos`) as a byte offset when slicing source.
2 parents 3087a1f + 8a64cf7 commit 26faee0

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/librustc_errors/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ impl CodeSuggestion {
9090
hi_opt: Option<&Loc>) {
9191
let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
9292
if let Some(line) = line_opt {
93-
if line.len() > lo {
93+
if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
94+
let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
9495
buf.push_str(match hi_opt {
9596
Some(hi) => &line[lo..hi],
9697
None => &line[lo..],
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
fn main() {
13+
let tup = (1,);
14+
println!("☃{}", tup[0]);
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: cannot index a value of type `({integer},)`
2+
--> $DIR/suggestion-non-ascii.rs:14:21
3+
|
4+
14 | println!("☃{}", tup[0]);
5+
| ^^^^^^
6+
|
7+
help: to access tuple elements, use tuple indexing syntax as shown
8+
| println!("☃{}", tup.0);
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)