aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/builtin_macro.rs
diff options
context:
space:
mode:
authorMarco Groppo <[email protected]>2019-11-24 18:02:04 +0000
committerMarco Groppo <[email protected]>2019-11-24 22:49:58 +0000
commitceb13a0494bed37b233bcaaeee70bfae5fefeccc (patch)
treed93fdd5f21c9cb04e0458e3f1921a9d3600016d9 /crates/ra_hir_expand/src/builtin_macro.rs
parent775bd98e5cf7918acf0dd72009ac14cf758ed0ca (diff)
Fix panic during the expansion of `column!`
Diffstat (limited to 'crates/ra_hir_expand/src/builtin_macro.rs')
-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