Skip to content

Commit 3890ba9

Browse files
authored
Rollup merge of #116630 - ouz-a:smir_span_better, r=oli-obk
Add ability to get lines/filename for Span in smir Wasn't sure about how to structure lines, so went with safest option, also I'm not sure why `span_to_lines` returns `vec`. Addresses rust-lang/project-stable-mir#44 r? ``@oli-obk``
2 parents 139f63a + d6a55d3 commit 3890ba9

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
1717
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1818
use rustc_target::abi::FieldIdx;
1919
use stable_mir::mir::{CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
20-
use stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy};
21-
use stable_mir::{self, opaque, Context};
20+
use stable_mir::ty::{
21+
FloatTy, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span, TyKind, UintTy,
22+
};
23+
use stable_mir::{self, opaque, Context, Filename};
2224
use tracing::debug;
2325

2426
mod alloc;
@@ -50,10 +52,27 @@ impl<'tcx> Context for Tables<'tcx> {
5052
self.tcx.def_path_str(self[def_id])
5153
}
5254

53-
fn print_span(&self, span: stable_mir::ty::Span) -> String {
55+
fn span_to_string(&self, span: stable_mir::ty::Span) -> String {
5456
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
5557
}
5658

59+
fn get_filename(&self, span: &Span) -> Filename {
60+
opaque(
61+
&self
62+
.tcx
63+
.sess
64+
.source_map()
65+
.span_to_filename(self[*span])
66+
.display(rustc_span::FileNameDisplayPreference::Local)
67+
.to_string(),
68+
)
69+
}
70+
71+
fn get_lines(&self, span: &Span) -> LineInfo {
72+
let lines = &self.tcx.sess.source_map().span_to_location_info(self[*span]);
73+
LineInfo { start_line: lines.1, start_col: lines.2, end_line: lines.3, end_col: lines.4 }
74+
}
75+
5776
fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind {
5877
self.tcx.def_kind(self[def_id]).stable(self)
5978
}

compiler/stable_mir/src/lib.rs

+13-6
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-
fn print_span(&self, span: Span) -> String;
203+
/// Returns printable, human readable form of `Span`
204+
fn span_to_string(&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) -> 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

compiler/stable_mir/src/ty.rs

+24-2
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)]
@@ -81,11 +81,33 @@ impl Debug for Span {
8181
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
8282
f.debug_struct("Span")
8383
.field("id", &self.0)
84-
.field("repr", &with(|cx| cx.print_span(*self)))
84+
.field("repr", &with(|cx| cx.span_to_string(*self)))
8585
.finish()
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) -> 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 start_line: usize,
106+
pub start_col: usize,
107+
pub end_line: usize,
108+
pub end_col: usize,
109+
}
110+
89111
impl IndexedVal for Span {
90112
fn to_val(index: usize) -> Self {
91113
Span(index)

0 commit comments

Comments
 (0)