From fa57a48cf589fd01f7cd9721f13281af8292168f Mon Sep 17 00:00:00 2001 From: ouz-a Date: Tue, 12 Sep 2023 11:50:50 +0300 Subject: [PATCH 1/2] span is index --- compiler/rustc_smir/src/rustc_internal/mod.rs | 17 ++++++++++++++++- compiler/rustc_smir/src/rustc_smir/mod.rs | 12 +++++++----- compiler/rustc_smir/src/stable_mir/mod.rs | 2 +- compiler/rustc_smir/src/stable_mir/ty.rs | 11 ++++++++++- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 4203990ab0560..10ee5af86c627 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -17,6 +17,7 @@ use rustc_interface::{interface, Queries}; use rustc_middle::mir::interpret::AllocId; use rustc_middle::ty::TyCtxt; pub use rustc_span::def_id::{CrateNum, DefId}; +use rustc_span::Span; fn with_tables(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R { let mut ret = None; @@ -159,6 +160,17 @@ impl<'tcx> Tables<'tcx> { self.alloc_ids.push(aid); stable_mir::AllocId(id) } + + pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span { + for (i, &sp) in self.spans.iter().enumerate() { + if sp == span { + return stable_mir::ty::Span(i); + } + } + let id = self.spans.len(); + self.spans.push(span); + stable_mir::ty::Span(id) + } } pub fn crate_num(item: &stable_mir::Crate) -> CrateNum { @@ -166,7 +178,10 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum { } pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) { - crate::stable_mir::run(Tables { tcx, def_ids: vec![], alloc_ids: vec![], types: vec![] }, f); + crate::stable_mir::run( + Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] }, + f, + ); } /// A type that provides internal information but that can still be used for debug purpose. diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 321ee7905093e..c5c32cd6e2bdf 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -9,7 +9,9 @@ use crate::rustc_internal::{self, opaque}; use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}; -use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy}; +use crate::stable_mir::ty::{ + FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy, +}; use crate::stable_mir::{self, CompilerError, Context}; use rustc_hir as hir; use rustc_middle::mir::interpret::{alloc_range, AllocId}; @@ -42,7 +44,7 @@ impl<'tcx> Context for Tables<'tcx> { self.tcx.def_path_str(self[def_id]) } - fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Span { + fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span { self.tcx.def_span(self[def_id]).stable(self) } @@ -168,6 +170,7 @@ pub struct Tables<'tcx> { pub tcx: TyCtxt<'tcx>, pub def_ids: Vec, pub alloc_ids: Vec, + pub spans: Vec, pub types: Vec>>, } @@ -1497,9 +1500,8 @@ impl<'tcx> Stable<'tcx> for ty::Region<'tcx> { impl<'tcx> Stable<'tcx> for rustc_span::Span { type T = stable_mir::ty::Span; - fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { - // FIXME: add a real implementation of stable spans - opaque(self) + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + tables.create_span(*self) } } diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index 508e3aa129136..09db44614e89f 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -90,7 +90,7 @@ impl CrateItem { with(|cx| cx.mir_body(self.0)) } - pub fn span(&self) -> ty::Span { + pub fn span(&self) -> Span { with(|cx| cx.span_of_an_item(self.0)) } } diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 54cba1263b7b1..5a63e13f8ab92 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -35,7 +35,16 @@ pub struct Const { type Ident = Opaque; pub(crate) type Region = Opaque; -pub(crate) type Span = Opaque; +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Span(pub(crate) usize); + +impl Debug for Span { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let mut span = None; + with(|context| context.rustc_tables(&mut |tables| span = Some(tables.spans[self.0]))); + f.write_fmt(format_args!("{:?}", &span.unwrap())) + } +} #[derive(Clone, Debug)] pub enum TyKind { From c397ca0677e31297f2be8a0391f3c7389618d05b Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 14 Sep 2023 13:01:54 +1000 Subject: [PATCH 2/2] Fix the error message for `#![feature(no_coverage)]` --- compiler/rustc_feature/src/removed.rs | 4 ++-- tests/ui/feature-gates/feature-gate-coverage-attribute.stderr | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_feature/src/removed.rs b/compiler/rustc_feature/src/removed.rs index c70e8e3e6b1ec..da18cb2a239e8 100644 --- a/compiler/rustc_feature/src/removed.rs +++ b/compiler/rustc_feature/src/removed.rs @@ -137,8 +137,8 @@ declare_features! ( /// Allows use of unary negate on unsigned integers, e.g., -e for e: u8 (removed, negate_unsigned, "1.0.0", Some(29645), None, None), /// Allows `#[no_coverage]` on functions. - /// The feature was renamed to `coverage` and the attribute to `#[coverage(on|off)]` - (removed, no_coverage, "CURRENT_RUSTC_VERSION", Some(84605), None, Some("renamed to `coverage`")), + /// The feature was renamed to `coverage_attribute` and the attribute to `#[coverage(on|off)]` + (removed, no_coverage, "CURRENT_RUSTC_VERSION", Some(84605), None, Some("renamed to `coverage_attribute`")), /// Allows `#[no_debug]`. (removed, no_debug, "1.43.0", Some(29721), None, Some("removed due to lack of demand")), /// Allows using `#[on_unimplemented(..)]` on traits. diff --git a/tests/ui/feature-gates/feature-gate-coverage-attribute.stderr b/tests/ui/feature-gates/feature-gate-coverage-attribute.stderr index 3912b9834fe24..0131a19a39dde 100644 --- a/tests/ui/feature-gates/feature-gate-coverage-attribute.stderr +++ b/tests/ui/feature-gates/feature-gate-coverage-attribute.stderr @@ -4,7 +4,7 @@ error[E0557]: feature has been removed LL | #![feature(no_coverage)] | ^^^^^^^^^^^ feature has been removed | - = note: renamed to `coverage` + = note: renamed to `coverage_attribute` error[E0658]: the `#[coverage]` attribute is an experimental feature --> $DIR/feature-gate-coverage-attribute.rs:10:1