Skip to content

Commit

Permalink
Unrolled build for rust-lang#116630
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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``
  • Loading branch information
rust-timer committed Oct 14, 2023
2 parents 495c5dd + d6a55d3 commit 2e86ba8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
25 changes: 22 additions & 3 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
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::ty::{
FloatTy, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span, TyKind, UintTy,
};
use stable_mir::{self, opaque, Context, Filename};
use tracing::debug;

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

fn print_span(&self, span: stable_mir::ty::Span) -> String {
fn span_to_string(&self, span: stable_mir::ty::Span) -> String {
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::Local)
.to_string(),
)
}

fn get_lines(&self, span: &Span) -> LineInfo {
let lines = &self.tcx.sess.source_map().span_to_location_info(self[*span]);
LineInfo { start_line: lines.1, start_col: lines.2, end_line: lines.3, end_col: lines.4 }
}

fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind {
self.tcx.def_kind(self[def_id]).stable(self)
}
Expand Down
19 changes: 13 additions & 6 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`
fn print_span(&self, span: Span) -> String;
/// Returns printable, human readable form of `Span`
fn span_to_string(&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) -> LineInfo;

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

/// `Span` of an item
Expand Down
26 changes: 24 additions & 2 deletions 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 @@ -81,11 +81,33 @@ impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Span")
.field("id", &self.0)
.field("repr", &with(|cx| cx.print_span(*self)))
.field("repr", &with(|cx| cx.span_to_string(*self)))
.finish()
}
}

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) -> 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 start_line: usize,
pub start_col: usize,
pub end_line: usize,
pub end_col: usize,
}

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

0 comments on commit 2e86ba8

Please sign in to comment.