Skip to content
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

Add ability to get lines/filename for Span in smir #116630

Merged
merged 2 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest that you return something similar to span_to_location_info.

It basically returns the line + column for beginning and end of the span.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified LineInfo struct so it looks like line,col:beg; line,col;end

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}
Expand Down
17 changes: 12 additions & 5 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use std::fmt;
use std::fmt::Debug;

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty,
TyKind,
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
TraitDef, Ty, TyKind,
};

#[macro_use]
Expand Down Expand Up @@ -108,6 +108,7 @@ pub struct Crate {
}

pub type DefKind = Opaque;
pub type Filename = Opaque;

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

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

/// Prints a human readable form of `Span`
/// Returns printable, human readable form of `Span`
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
fn print_span(&self, span: Span) -> String;

/// Prints the kind of given `DefId`
/// Return filename from given `Span`, for diagnostic purposes
fn get_filename(&self, span: &Span) -> Filename;

/// Return lines corresponding to this `Span`
fn get_lines(&self, span: &Span) -> Vec<LineInfo>;

/// Returns the `kind` of given `DefId`
fn def_kind(&mut self, def_id: DefId) -> DefKind;

/// `Span` of an item
Expand Down
23 changes: 22 additions & 1 deletion compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document whether the first line is 0 or whether it is 1. Similarly for col

Copy link
Contributor Author

@ouz-a ouz-a Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made them start from 1 now

pub start_col: usize,
pub end_col: usize,
}

impl IndexedVal for Span {
fn to_val(index: usize) -> Self {
Span(index)
Expand Down
Loading