Skip to content

Commit 1ee5592

Browse files
committed
Expand column!()
1 parent 506131e commit 1ee5592

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

crates/ra_hir/src/ty/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,3 +4876,22 @@ fn main() {
48764876
"###
48774877
);
48784878
}
4879+
4880+
#[test]
4881+
fn infer_builtin_macros_column() {
4882+
assert_snapshot!(
4883+
infer(r#"
4884+
#[rustc_builtin_macro]
4885+
macro_rules! column {() => {}}
4886+
4887+
fn main() {
4888+
let x = column!();
4889+
}
4890+
"#),
4891+
@r###"
4892+
![0; 2) '13': i32
4893+
[66; 92) '{ ...!(); }': ()
4894+
[76; 77) 'x': i32
4895+
"###
4896+
);
4897+
}

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::quote;
1010

1111
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1212
pub enum BuiltinExpander {
13+
Column,
1314
File,
1415
Line,
1516
Stringify,
@@ -23,6 +24,7 @@ impl BuiltinExpander {
2324
tt: &tt::Subtree,
2425
) -> Result<tt::Subtree, mbe::ExpandError> {
2526
match self {
27+
BuiltinExpander::Column => column_expand(db, id, tt),
2628
BuiltinExpander::File => file_expand(db, id, tt),
2729
BuiltinExpander::Line => line_expand(db, id, tt),
2830
BuiltinExpander::Stringify => stringify_expand(db, id, tt),
@@ -36,7 +38,9 @@ pub fn find_builtin_macro(
3638
ast_id: AstId<ast::MacroCall>,
3739
) -> Option<MacroDefId> {
3840
// FIXME: Better registering method
39-
if ident == &name::FILE_MACRO {
41+
if ident == &name::COLUMN_MACRO {
42+
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Column) })
43+
} else if ident == &name::FILE_MACRO {
4044
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::File) })
4145
} else if ident == &name::LINE_MACRO {
4246
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
@@ -110,13 +114,51 @@ fn stringify_expand(
110114
Ok(expanded)
111115
}
112116

117+
fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
118+
// FIXME: Use expansion info
119+
let file_id = file.original_file(db);
120+
let text = db.file_text(file_id);
121+
let mut col_num = 1;
122+
123+
for c in text[..pos.to_usize()].chars().rev() {
124+
if c == '\n' {
125+
break;
126+
}
127+
col_num = col_num + 1;
128+
}
129+
130+
col_num
131+
}
132+
133+
fn column_expand(
134+
db: &dyn AstDatabase,
135+
id: MacroCallId,
136+
_tt: &tt::Subtree,
137+
) -> Result<tt::Subtree, mbe::ExpandError> {
138+
let loc = db.lookup_intern_macro(id);
139+
let macro_call = loc.ast_id.to_node(db);
140+
141+
let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
142+
let col_start = macro_call.syntax().text_range().start();
143+
144+
let file = id.as_file(MacroFileKind::Expr);
145+
let col_num = to_col_number(db, file, col_start);
146+
147+
let expanded = quote! {
148+
#col_num
149+
};
150+
151+
Ok(expanded)
152+
}
153+
113154
fn file_expand(
114155
db: &dyn AstDatabase,
115156
id: MacroCallId,
116157
_tt: &tt::Subtree,
117158
) -> Result<tt::Subtree, mbe::ExpandError> {
118159
let loc = db.lookup_intern_macro(id);
119160
let macro_call = loc.ast_id.to_node(db);
161+
120162
let _ = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
121163

122164
// FIXME: RA purposefully lacks knowledge of absolute file names

crates/ra_hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,6 @@ pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143143

144144
// Builtin Macros
145145
pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file");
146+
pub const COLUMN_MACRO: Name = Name::new_inline_ascii(6, b"column");
146147
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
147148
pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");

0 commit comments

Comments
 (0)