aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-25 02:42:17 +0000
committerGitHub <[email protected]>2019-11-25 02:42:17 +0000
commita888441c4745c3d72bee737c69873dfc9f52f4bb (patch)
treec1593cd79a2c0d111f62898f62126c5b01cf9692
parentf7f9757b6b144385ab8ce57b15764473b1f57331 (diff)
parentceb13a0494bed37b233bcaaeee70bfae5fefeccc (diff)
Merge #2392
2392: Fix panic during the expansion of `column!` r=edwin0cheng a=marcogroppo Fixes #2379. Well, this isn't the "proper" fix but it doesn't hurt, IMHO. The problem is that `to_col_number`, called by `column_expand`, receives a position number that isn't included in the text range of the file. My (very limited) understanding is that the text is the one of the original file, while `pos` is relative to something else, probably the text of the macro. Notice that in this case the `column!` expansion seems to be triggered by `assert_eq!`, so we're in the middle of another expansion. This PR simply avoids the panic by checking the length of the text. r? @edwin0cheng Co-authored-by: Marco Groppo <[email protected]>
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index c0e0436c0..9b5305a80 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -57,16 +57,21 @@ fn to_line_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize
57 let text = db.file_text(file_id); 57 let text = db.file_text(file_id);
58 let mut line_num = 1; 58 let mut line_num = 1;
59 59
60 let pos = pos.to_usize();
61 if pos > text.len() {
62 // FIXME: `pos` at the moment could be an offset inside the "wrong" file
63 // in this case, when we know it's wrong, we return a dummy value
64 return 0;
65 }
60 // Count line end 66 // Count line end
61 for (i, c) in text.chars().enumerate() { 67 for (i, c) in text.chars().enumerate() {
62 if i == pos.to_usize() { 68 if i == pos {
63 break; 69 break;
64 } 70 }
65 if c == '\n' { 71 if c == '\n' {
66 line_num += 1; 72 line_num += 1;
67 } 73 }
68 } 74 }
69
70 line_num 75 line_num
71} 76}
72 77
@@ -118,15 +123,21 @@ fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize
118 // FIXME: Use expansion info 123 // FIXME: Use expansion info
119 let file_id = file.original_file(db); 124 let file_id = file.original_file(db);
120 let text = db.file_text(file_id); 125 let text = db.file_text(file_id);
121 let mut col_num = 1;
122 126
123 for c in text[..pos.to_usize()].chars().rev() { 127 let pos = pos.to_usize();
128 if pos > text.len() {
129 // FIXME: `pos` at the moment could be an offset inside the "wrong" file
130 // in this case we return a dummy value so that we don't `panic!`
131 return 0;
132 }
133
134 let mut col_num = 1;
135 for c in text[..pos].chars().rev() {
124 if c == '\n' { 136 if c == '\n' {
125 break; 137 break;
126 } 138 }
127 col_num = col_num + 1; 139 col_num = col_num + 1;
128 } 140 }
129
130 col_num 141 col_num
131} 142}
132 143