rustc_utils/source_map/
range.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::{
  cell::RefCell, collections::hash_map::Entry, default::Default, ffi::OsStr,
  path::PathBuf,
};

use anyhow::{bail, ensure, Context, Result};
use rustc_data_structures::{fx::FxHashMap as HashMap, sync::Lrc};
use rustc_hir::{
  intravisit::{self, Visitor},
  BodyId,
};
use rustc_index::IndexVec;
use rustc_middle::ty::TyCtxt;
use rustc_span::{source_map::SourceMap, FileName, RealFileName, SourceFile, Span};
#[cfg(feature = "serde")]
use serde::Serialize;
#[cfg(feature = "ts-rs")]
use ts_rs::TS;

use super::filename::{Filename, FilenameIndex};
use crate::cache::Cache;

struct CharByteMapping {
  byte_to_char: HashMap<BytePos, CharPos>,
  char_to_byte: HashMap<CharPos, BytePos>,
}

impl CharByteMapping {
  pub fn build(file: &SourceFile) -> Self {
    let mut byte_to_char = HashMap::default();
    let mut char_to_byte = HashMap::default();

    macro_rules! check_insert {
      ($bpos:expr, $cpos:expr) => {
        let _r = byte_to_char.insert($bpos, $cpos);
        debug_assert!(
          _r.is_none(),
          "byte_to_char: bytepos={:?}, charpos={:?}",
          $bpos,
          $cpos
        );
        let _r = char_to_byte.insert($cpos, $bpos);
        debug_assert!(
          _r.is_none(),
          "char_to_byte: bytepos={:?}, charpos={:?}",
          $bpos,
          $cpos
        );
      };
    }

    for line in 0 .. file.count_lines() {
      let line_str = file.get_line(line).unwrap();
      let line_bounds = file.line_bounds(line);
      let line_start = line_bounds.start.0 as usize;
      let mut last_column = 0;
      let mut last_offset = 0;
      for (column, (byte_offset, c)) in line_str.char_indices().enumerate() {
        let bpos = BytePos(line_start + byte_offset);
        let cpos = CharPos { line, column };
        check_insert!(bpos, cpos);
        last_column = column + 1;
        last_offset = byte_offset + c.len_utf8();
      }

      let bpos = BytePos(line_start + last_offset);
      let cpos = CharPos {
        line,
        column: last_column,
      };
      check_insert!(bpos, cpos);
    }

    CharByteMapping {
      byte_to_char,
      char_to_byte,
    }
  }

  #[allow(unused)]
  pub fn byte_to_char(&self, pos: BytePos) -> CharPos {
    *self
      .byte_to_char
      .get(&pos)
      .unwrap_or_else(|| panic!("Could not find char pos for {pos:?}"))
  }

  pub fn char_to_byte(&self, pos: CharPos) -> BytePos {
    *self
      .char_to_byte
      .get(&pos)
      .unwrap_or_else(|| panic!("Could not find byte pos for {pos:?}"))
  }
}

#[derive(Default)]
pub struct RangeContext {
  filenames: IndexVec<FilenameIndex, Filename>,
  path_mapping: HashMap<FilenameIndex, Lrc<SourceFile>>,
  char_byte_mapping: Cache<FilenameIndex, CharByteMapping>,
}

thread_local! {
  static CONTEXT: RefCell<RangeContext> = RefCell::new(RangeContext::default());
}

impl Filename {
  fn intern_with_ctx(self, ctx: &mut RangeContext) -> FilenameIndex {
    let existing = ctx.filenames.iter_enumerated().find(|(_, f)| &self == *f);
    match existing {
      Some((index, _)) => index,
      None => ctx.filenames.push(self),
    }
  }

  pub fn intern<T: ?Sized + AsRef<OsStr>>(t: &T) -> FilenameIndex {
    let filename = Filename(PathBuf::from(t));
    CONTEXT.with(|ctx| filename.intern_with_ctx(&mut ctx.borrow_mut()))
  }
}

impl FilenameIndex {
  pub fn find_source_file(self, source_map: &SourceMap) -> Result<Lrc<SourceFile>> {
    CONTEXT.with(|ctx| {
      let ctx = &mut *ctx.borrow_mut();
      match ctx.path_mapping.entry(self) {
        Entry::Occupied(entry) => Ok(Lrc::clone(entry.get())),
        Entry::Vacant(entry) => {
          let files = source_map.files();
          ensure!(
            ctx.filenames.get(self).is_some(),
            "Missing file index!"
          );
          let filename = &ctx.filenames[self];
          let filename = filename.0
            .canonicalize()
            .unwrap_or_else(|_| filename.0.clone());
          let rustc_filename = files
            .iter()
            .map(|file| &file.name)
            .find(|name| match &name {
              // rustc seems to store relative paths to files in the workspace, so if filename is absolute,
              // we can compare them using Path::ends_with
              FileName::Real(RealFileName::LocalPath(other)) => {
                let canonical = other.canonicalize();
                let other = canonical.as_ref().unwrap_or(other);
                filename.ends_with(other)
              }
              _ => false,
            })
            .with_context(|| {
              format!(
                "Could not find SourceFile for path: {}. Available SourceFiles were: [{}]",
                filename.display(),
                files
                  .iter()
                  .filter_map(|file| match &file.name {
                    FileName::Real(RealFileName::LocalPath(other)) =>
                      Some(format!("{}", other.display())),
                    _ => None,
                  })
                  .collect::<Vec<_>>()
                  .join(", ")
              )
            })?;
          let file = source_map.get_source_file(rustc_filename).unwrap();
          entry.insert(Lrc::clone(&file));
          Ok(file)
        }
      }
    })
  }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "ts-rs", derive(TS))]
pub struct BytePos(pub usize);

/// CharPos is designed to match VSCode's vscode.Position type.
/// Both line and column are 0-based.
///
/// A previous version of CharPos used a global character-based index,
/// naively thinking this was the same as VSCode's notion of an "offset".
/// However, for files using CRLF line endings, VSCode ignores the `\r`
/// when computing offsets, while Rustc does not. The unhappy compromise
/// is to use line-column as a common coordinate system, which is robust
/// to choice of line endings.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "ts-rs", derive(TS))]
pub struct CharPos {
  pub line: usize,
  pub column: usize,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "ts-rs", derive(TS))]
pub struct ByteRange {
  pub start: BytePos,
  pub end: BytePos,
  pub filename: FilenameIndex,
}

/// Data structure for sharing spans outside rustc.
///
/// Rustc uses byte indexes to describe ranges of source code, whereas
/// most Javascript-based editors I've encountered (e.g. VSCode) use
/// character-based (really grapheme-based) indexes. This data structure
/// along with [`ByteRange`] helps convert between the two representations.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "ts-rs", derive(TS))]
pub struct CharRange {
  pub start: CharPos,
  pub end: CharPos,
  pub filename: FilenameIndex,
}

impl ByteRange {
  pub fn as_char_range(&self, source_map: &SourceMap) -> CharRange {
    let file = self.filename.find_source_file(source_map).unwrap();

    CONTEXT.with(|ctx| {
      let ctx = ctx.borrow();
      let mapping: &CharByteMapping = ctx
        .char_byte_mapping
        .get(&self.filename, |_| CharByteMapping::build(&file));

      let char_start = mapping.byte_to_char(self.start);
      let char_end = mapping.byte_to_char(self.end);

      CharRange {
        start: char_start,
        end: char_end,
        filename: self.filename,
      }
    })
  }

  pub fn from_char_range(
    char_start: CharPos,
    char_end: CharPos,
    filename: FilenameIndex,
    source_map: &SourceMap,
  ) -> Result<ByteRange> {
    let file = filename.find_source_file(source_map)?;

    CONTEXT.with(|ctx| {
      let ctx = ctx.borrow();
      let mapping = ctx
        .char_byte_mapping
        .get(&filename, |_| CharByteMapping::build(&file));
      let byte_start = mapping.char_to_byte(char_start);
      let byte_end = mapping.char_to_byte(char_end);
      Ok(ByteRange {
        start: byte_start,
        end: byte_end,
        filename,
      })
    })
  }

  pub fn from_span(span: Span, source_map: &SourceMap) -> Result<Self> {
    CONTEXT.with(|ctx| {
      let mut ctx = ctx.borrow_mut();

      log::trace!("Converting to range: {span:?}");
      let file = source_map.lookup_source_file(span.lo());
      let filename = match &file.name {
        FileName::Real(RealFileName::LocalPath(filename)) => {
          Filename(filename.clone()).intern_with_ctx(&mut ctx)
        }
        filename => bail!("Range::from_span doesn't support {filename:?}"),
      };

      ensure!(
        source_map.ensure_source_file_source_present(&file),
        "Could not load source for file: {:?}",
        file.name
      );
      let external = file.external_src.borrow();
      let _src = file
        .src
        .as_deref()
        .map(String::as_str)
        .unwrap_or_else(|| external.get_source().as_ref().unwrap());

      let byte_start = BytePos(span.lo().0 as usize);
      let byte_end = BytePos(span.hi().0 as usize);

      Ok(ByteRange {
        start: byte_start,
        end: byte_end,
        filename,
      })
    })
  }

  pub fn substr(&self, s: &str) -> String {
    s[self.start.0 .. self.end.0].to_string()
  }
}

impl CharRange {
  pub fn from_span(span: Span, source_map: &SourceMap) -> Result<Self> {
    let byte_range = ByteRange::from_span(span, source_map)?;
    Ok(byte_range.as_char_range(source_map))
  }
}

/// Used to convert objects into a [`Span`] with access to [`TyCtxt`]
pub trait ToSpan {
  fn to_span(&self, tcx: TyCtxt) -> Result<Span>;
}

impl ToSpan for ByteRange {
  fn to_span(&self, _tcx: TyCtxt) -> Result<Span> {
    Ok(Span::with_root_ctxt(
      rustc_span::BytePos(u32::try_from(self.start.0).unwrap()),
      rustc_span::BytePos(u32::try_from(self.end.0).unwrap()),
    ))
  }
}

impl ToSpan for CharRange {
  fn to_span(&self, tcx: TyCtxt) -> Result<Span> {
    let range = ByteRange::from_char_range(
      self.start,
      self.end,
      self.filename,
      tcx.sess.source_map(),
    )?;
    range.to_span(tcx)
  }
}

fn qpath_to_span(tcx: TyCtxt, qpath: String) -> Result<Span> {
  struct Finder<'tcx> {
    tcx: TyCtxt<'tcx>,
    qpath: String,
    span: Option<Span>,
  }

  impl<'tcx> Visitor<'tcx> for Finder<'tcx> {
    fn visit_nested_body(&mut self, id: BodyId) {
      intravisit::walk_body(self, self.tcx.hir().body(id));

      let local_def_id = self.tcx.hir().body_owner_def_id(id);
      let function_path = self
        .tcx
        .def_path(local_def_id.to_def_id())
        .to_string_no_crate_verbose();
      if function_path[2 ..] == self.qpath {
        self.span = Some(self.tcx.hir().span(id.hir_id));
      }
    }
  }

  let mut finder = Finder {
    tcx,
    qpath,
    span: None,
  };
  tcx.hir().visit_all_item_likes_in_crate(&mut finder);
  finder
    .span
    .with_context(|| format!("No function with qpath {}", finder.qpath))
}

/// An externally-provided identifier of a function
pub enum FunctionIdentifier {
  /// Name of a function
  Qpath(String),

  /// Range of code possibly inside a function
  Range(CharRange),
}

impl ToSpan for FunctionIdentifier {
  fn to_span(&self, tcx: TyCtxt) -> Result<Span> {
    match self {
      FunctionIdentifier::Qpath(qpath) => qpath_to_span(tcx, qpath.clone()),
      FunctionIdentifier::Range(range) => range.to_span(tcx),
    }
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use crate::test_utils::{self, CompileResult};

  #[test]
  fn test_range() {
    let emoji = "🦀";
    let input = &format!(
      r#"fn main() {{
  let x = "{emoji}";
}}

// mysterious bytes
"#
    );

    test_utils::CompileBuilder::new(input).compile(|CompileResult { tcx, .. }| {
      let source_map = tcx.sess.source_map();
      let filename = Filename::intern("dummy.rs");
      filename.find_source_file(source_map).unwrap();

      let id = FunctionIdentifier::Qpath(String::from("main"));
      id.to_span(tcx).unwrap();

      let id = FunctionIdentifier::Qpath(String::from("foobar"));
      id.to_span(tcx).unwrap_err();

      let id = FunctionIdentifier::Range(CharRange {
        start: CharPos { line: 0, column: 0 },
        end: CharPos { line: 0, column: 1 },
        filename,
      });
      id.to_span(tcx).unwrap();

      let emoji_index = input.find(emoji).unwrap();
      let byte_range = ByteRange {
        start: BytePos(emoji_index),
        end: BytePos(emoji_index + emoji.len()),
        filename,
      };
      let char_range = byte_range.as_char_range(source_map);
      let emoji_line = 1;
      let emoji_column = 11;
      assert_eq!(char_range, CharRange {
        start: CharPos {
          line: emoji_line,
          column: emoji_column
        },
        end: CharPos {
          line: emoji_line,
          column: emoji_column + 1
        },
        filename
      });
    });
  }
}