Skip to content

Commit 1d9481f

Browse files
committed
implement get_filename/lines for span
1 parent 6d05c43 commit 1d9481f

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

Diff for: compiler/rustc_smir/src/rustc_smir/mod.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1818
use rustc_target::abi::FieldIdx;
1919
use stable_mir::mir::{CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
2020
use stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy};
21-
use stable_mir::{self, opaque, Context};
21+
use stable_mir::{self, opaque, Context, Filename};
2222
use tracing::debug;
2323

2424
mod alloc;
@@ -54,6 +54,36 @@ impl<'tcx> Context for Tables<'tcx> {
5454
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
5555
}
5656

57+
fn get_filename(&self, span: &Span) -> Filename {
58+
opaque(
59+
&self
60+
.tcx
61+
.sess
62+
.source_map()
63+
.span_to_filename(self[*span])
64+
.display(rustc_span::FileNameDisplayPreference::Short)
65+
.to_string(),
66+
)
67+
}
68+
69+
fn get_lines(&self, span: &Span) -> Vec<stable_mir::ty::LineInfo> {
70+
let lines = &self
71+
.tcx
72+
.sess
73+
.source_map()
74+
.span_to_lines(self[*span])
75+
.unwrap()
76+
.lines
77+
.iter()
78+
.map(|line| stable_mir::ty::LineInfo {
79+
line_index: line.line_index + 1,
80+
start_col: line.start_col.0 + 1,
81+
end_col: line.end_col.0 + 1,
82+
})
83+
.collect::<Vec<stable_mir::ty::LineInfo>>();
84+
lines.to_vec()
85+
}
86+
5787
fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind {
5888
self.tcx.def_kind(self[def_id]).stable(self)
5989
}

Diff for: compiler/stable_mir/src/lib.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use std::fmt;
2222
use std::fmt::Debug;
2323

2424
use self::ty::{
25-
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty,
26-
TyKind,
25+
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
26+
TraitDef, Ty, TyKind,
2727
};
2828

2929
#[macro_use]
@@ -108,6 +108,7 @@ pub struct Crate {
108108
}
109109

110110
pub type DefKind = Opaque;
111+
pub type Filename = Opaque;
111112

112113
/// Holds information about an item in the crate.
113114
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
@@ -196,13 +197,19 @@ pub trait Context {
196197
/// Find a crate with the given name.
197198
fn find_crates(&self, name: &str) -> Vec<Crate>;
198199

199-
/// Prints the name of given `DefId`
200+
/// Returns the name of given `DefId`
200201
fn name_of_def_id(&self, def_id: DefId) -> String;
201202

202-
/// Prints a human readable form of `Span`
203+
/// Returns printable, human readable form of `Span`
203204
fn print_span(&self, span: Span) -> String;
204205

205-
/// Prints the kind of given `DefId`
206+
/// Return filename from given `Span`, for diagnostic purposes
207+
fn get_filename(&self, span: &Span) -> Filename;
208+
209+
/// Return lines corresponding to this `Span`
210+
fn get_lines(&self, span: &Span) -> Vec<LineInfo>;
211+
212+
/// Returns the `kind` of given `DefId`
206213
fn def_kind(&mut self, def_id: DefId) -> DefKind;
207214

208215
/// `Span` of an item

Diff for: compiler/stable_mir/src/ty.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{
33
mir::{Body, Mutability},
44
with, AllocId, DefId, Symbol,
55
};
6-
use crate::Opaque;
6+
use crate::{Filename, Opaque};
77
use std::fmt::{self, Debug, Formatter};
88

99
#[derive(Copy, Clone)]
@@ -86,6 +86,27 @@ impl Debug for Span {
8686
}
8787
}
8888

89+
impl Span {
90+
/// Return filename for diagnostic purposes
91+
pub fn get_filename(&self) -> Filename {
92+
with(|c| c.get_filename(self))
93+
}
94+
95+
/// Return lines that corespond to this `Span`
96+
pub fn get_lines(&self) -> Vec<LineInfo> {
97+
with(|c| c.get_lines(&self))
98+
}
99+
}
100+
101+
#[derive(Clone, Copy, Debug)]
102+
/// Information you get from `Span` in a struct form.
103+
/// Line and col start from 1.
104+
pub struct LineInfo {
105+
pub line_index: usize,
106+
pub start_col: usize,
107+
pub end_col: usize,
108+
}
109+
89110
impl IndexedVal for Span {
90111
fn to_val(index: usize) -> Self {
91112
Span(index)

0 commit comments

Comments
 (0)