-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add ability to get lines/filename for Span in smir #116630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; | |
use rustc_target::abi::FieldIdx; | ||
use stable_mir::mir::{CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx}; | ||
use stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy}; | ||
use stable_mir::{self, opaque, Context}; | ||
use stable_mir::{self, opaque, Context, Filename}; | ||
use tracing::debug; | ||
|
||
mod alloc; | ||
|
@@ -54,6 +54,36 @@ impl<'tcx> Context for Tables<'tcx> { | |
self.tcx.sess.source_map().span_to_diagnostic_string(self[span]) | ||
} | ||
|
||
fn get_filename(&self, span: &Span) -> Filename { | ||
opaque( | ||
&self | ||
.tcx | ||
.sess | ||
.source_map() | ||
.span_to_filename(self[*span]) | ||
.display(rustc_span::FileNameDisplayPreference::Short) | ||
.to_string(), | ||
) | ||
} | ||
|
||
fn get_lines(&self, span: &Span) -> Vec<stable_mir::ty::LineInfo> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest that you return something similar to It basically returns the line + column for beginning and end of the span. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modified There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect! Thanks |
||
let lines = &self | ||
.tcx | ||
.sess | ||
.source_map() | ||
.span_to_lines(self[*span]) | ||
.unwrap() | ||
.lines | ||
.iter() | ||
.map(|line| stable_mir::ty::LineInfo { | ||
line_index: line.line_index + 1, | ||
start_col: line.start_col.0 + 1, | ||
end_col: line.end_col.0 + 1, | ||
}) | ||
.collect::<Vec<stable_mir::ty::LineInfo>>(); | ||
lines.to_vec() | ||
} | ||
|
||
fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind { | ||
self.tcx.def_kind(self[def_id]).stable(self) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ use super::{ | |
mir::{Body, Mutability}, | ||
with, AllocId, DefId, Symbol, | ||
}; | ||
use crate::Opaque; | ||
use crate::{Filename, Opaque}; | ||
use std::fmt::{self, Debug, Formatter}; | ||
|
||
#[derive(Copy, Clone)] | ||
|
@@ -86,6 +86,27 @@ impl Debug for Span { | |
} | ||
} | ||
|
||
impl Span { | ||
/// Return filename for diagnostic purposes | ||
pub fn get_filename(&self) -> Filename { | ||
with(|c| c.get_filename(self)) | ||
} | ||
|
||
/// Return lines that corespond to this `Span` | ||
pub fn get_lines(&self) -> Vec<LineInfo> { | ||
with(|c| c.get_lines(&self)) | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
/// Information you get from `Span` in a struct form. | ||
/// Line and col start from 1. | ||
pub struct LineInfo { | ||
pub line_index: usize, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document whether the first line is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made them start from |
||
pub start_col: usize, | ||
pub end_col: usize, | ||
} | ||
|
||
impl IndexedVal for Span { | ||
fn to_val(index: usize) -> Self { | ||
Span(index) | ||
|
Uh oh!
There was an error while loading. Please reload this page.