1
Fork 0

try apply rustc_pass_by_value to Span

This commit is contained in:
lcnr 2022-01-27 08:17:13 +01:00
parent e6808381e5
commit 2684dfe583
8 changed files with 32 additions and 23 deletions

View file

@ -607,7 +607,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
&mut self, &mut self,
macro_def: &ast::MacroDef, macro_def: &ast::MacroDef,
ident: &Ident, ident: &Ident,
sp: &Span, sp: Span,
print_visibility: impl FnOnce(&mut Self), print_visibility: impl FnOnce(&mut Self),
) { ) {
let (kw, has_bang) = if macro_def.macro_rules { let (kw, has_bang) = if macro_def.macro_rules {
@ -623,7 +623,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
macro_def.body.delim(), macro_def.body.delim(),
&macro_def.body.inner_tokens(), &macro_def.body.inner_tokens(),
true, true,
*sp, sp,
); );
if macro_def.body.need_semicolon() { if macro_def.body.need_semicolon() {
self.word(";"); self.word(";");

View file

@ -347,7 +347,7 @@ impl<'a> State<'a> {
} }
} }
ast::ItemKind::MacroDef(ref macro_def) => { ast::ItemKind::MacroDef(ref macro_def) => {
self.print_mac_def(macro_def, &item.ident, &item.span, |state| { self.print_mac_def(macro_def, &item.ident, item.span, |state| {
state.print_visibility(&item.vis) state.print_visibility(&item.vis)
}); });
} }

View file

@ -571,7 +571,7 @@ impl<'a> State<'a> {
self.ann.nested(self, Nested::Body(body)); self.ann.nested(self, Nested::Body(body));
} }
hir::ItemKind::Macro(ref macro_def) => { hir::ItemKind::Macro(ref macro_def) => {
self.print_mac_def(macro_def, &item.ident, &item.span, |state| { self.print_mac_def(macro_def, &item.ident, item.span, |state| {
state.print_visibility(&item.vis) state.print_visibility(&item.vis)
}); });
} }

View file

@ -336,5 +336,5 @@ fn is_arg_inside_call(arg: Span, call: Span) -> bool {
// panic call in the source file, to avoid invalid suggestions when macros are involved. // panic call in the source file, to avoid invalid suggestions when macros are involved.
// We specifically check for the spans to not be identical, as that happens sometimes when // We specifically check for the spans to not be identical, as that happens sometimes when
// proc_macros lie about spans and apply the same span to all the tokens they produce. // proc_macros lie about spans and apply the same span to all the tokens they produce.
call.contains(arg) && !call.source_equal(&arg) call.contains(arg) && !call.source_equal(arg)
} }

View file

@ -230,7 +230,7 @@ where
} }
/// Format a string showing the start line and column, and end line and column within a file. /// Format a string showing the start line and column, and end line and column within a file.
pub fn source_range_no_file<'tcx>(tcx: TyCtxt<'tcx>, span: &Span) -> String { pub fn source_range_no_file<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
let source_map = tcx.sess.source_map(); let source_map = tcx.sess.source_map();
let start = source_map.lookup_char_pos(span.lo()); let start = source_map.lookup_char_pos(span.lo());
let end = source_map.lookup_char_pos(span.hi()); let end = source_map.lookup_char_pos(span.hi());
@ -629,7 +629,7 @@ fn tooltip<'tcx>(
let mut text = Vec::new(); let mut text = Vec::new();
text.push(format!("{}: {}:", spanview_id, &source_map.span_to_embeddable_string(span))); text.push(format!("{}: {}:", spanview_id, &source_map.span_to_embeddable_string(span)));
for statement in statements { for statement in statements {
let source_range = source_range_no_file(tcx, &statement.source_info.span); let source_range = source_range_no_file(tcx, statement.source_info.span);
text.push(format!( text.push(format!(
"\n{}{}: {}: {:?}", "\n{}{}: {}: {:?}",
TOOLTIP_INDENT, TOOLTIP_INDENT,
@ -639,7 +639,7 @@ fn tooltip<'tcx>(
)); ));
} }
if let Some(term) = terminator { if let Some(term) = terminator {
let source_range = source_range_no_file(tcx, &term.source_info.span); let source_range = source_range_no_file(tcx, term.source_info.span);
text.push(format!( text.push(format!(
"\n{}{}: {}: {:?}", "\n{}{}: {}: {:?}",
TOOLTIP_INDENT, TOOLTIP_INDENT,

View file

@ -28,7 +28,7 @@ impl CoverageStatement {
let stmt = &mir_body[bb].statements[stmt_index]; let stmt = &mir_body[bb].statements[stmt_index];
format!( format!(
"{}: @{}[{}]: {:?}", "{}: @{}[{}]: {:?}",
source_range_no_file(tcx, &span), source_range_no_file(tcx, span),
bb.index(), bb.index(),
stmt_index, stmt_index,
stmt stmt
@ -38,7 +38,7 @@ impl CoverageStatement {
let term = mir_body[bb].terminator(); let term = mir_body[bb].terminator();
format!( format!(
"{}: @{}.{}: {:?}", "{}: @{}.{}: {:?}",
source_range_no_file(tcx, &span), source_range_no_file(tcx, span),
bb.index(), bb.index(),
term_type(&term.kind), term_type(&term.kind),
term.kind term.kind
@ -155,7 +155,7 @@ impl CoverageSpan {
pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String { pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String {
format!( format!(
"{}\n {}", "{}\n {}",
source_range_no_file(tcx, &self.span), source_range_no_file(tcx, self.span),
self.format_coverage_statements(tcx, mir_body).replace('\n', "\n "), self.format_coverage_statements(tcx, mir_body).replace('\n', "\n "),
) )
} }

View file

@ -611,7 +611,7 @@ impl Span {
#[inline] #[inline]
/// Returns `true` if `hi == lo`. /// Returns `true` if `hi == lo`.
pub fn is_empty(&self) -> bool { pub fn is_empty(self) -> bool {
let span = self.data_untracked(); let span = self.data_untracked();
span.hi == span.lo span.hi == span.lo
} }
@ -639,7 +639,7 @@ impl Span {
/// ///
/// Use this instead of `==` when either span could be generated code, /// Use this instead of `==` when either span could be generated code,
/// and you only care that they point to the same bytes of source text. /// and you only care that they point to the same bytes of source text.
pub fn source_equal(&self, other: &Span) -> bool { pub fn source_equal(self, other: Span) -> bool {
let span = self.data(); let span = self.data();
let other = other.data(); let other = other.data();
span.lo == other.lo && span.hi == other.hi span.lo == other.lo && span.hi == other.hi
@ -680,17 +680,17 @@ impl Span {
} }
#[inline] #[inline]
pub fn rust_2015(&self) -> bool { pub fn rust_2015(self) -> bool {
self.edition() == edition::Edition::Edition2015 self.edition() == edition::Edition::Edition2015
} }
#[inline] #[inline]
pub fn rust_2018(&self) -> bool { pub fn rust_2018(self) -> bool {
self.edition() >= edition::Edition::Edition2018 self.edition() >= edition::Edition::Edition2018
} }
#[inline] #[inline]
pub fn rust_2021(&self) -> bool { pub fn rust_2021(self) -> bool {
self.edition() >= edition::Edition::Edition2021 self.edition() >= edition::Edition::Edition2021
} }
@ -711,7 +711,7 @@ impl Span {
/// Checks if a span is "internal" to a macro in which `#[unstable]` /// Checks if a span is "internal" to a macro in which `#[unstable]`
/// items can be used (that is, a macro marked with /// items can be used (that is, a macro marked with
/// `#[allow_internal_unstable]`). /// `#[allow_internal_unstable]`).
pub fn allows_unstable(&self, feature: Symbol) -> bool { pub fn allows_unstable(self, feature: Symbol) -> bool {
self.ctxt() self.ctxt()
.outer_expn_data() .outer_expn_data()
.allow_internal_unstable .allow_internal_unstable
@ -719,7 +719,7 @@ impl Span {
} }
/// Checks if this span arises from a compiler desugaring of kind `kind`. /// Checks if this span arises from a compiler desugaring of kind `kind`.
pub fn is_desugaring(&self, kind: DesugaringKind) -> bool { pub fn is_desugaring(self, kind: DesugaringKind) -> bool {
match self.ctxt().outer_expn_data().kind { match self.ctxt().outer_expn_data().kind {
ExpnKind::Desugaring(k) => k == kind, ExpnKind::Desugaring(k) => k == kind,
_ => false, _ => false,
@ -728,7 +728,7 @@ impl Span {
/// Returns the compiler desugaring that created this span, or `None` /// Returns the compiler desugaring that created this span, or `None`
/// if this span is not from a desugaring. /// if this span is not from a desugaring.
pub fn desugaring_kind(&self) -> Option<DesugaringKind> { pub fn desugaring_kind(self) -> Option<DesugaringKind> {
match self.ctxt().outer_expn_data().kind { match self.ctxt().outer_expn_data().kind {
ExpnKind::Desugaring(k) => Some(k), ExpnKind::Desugaring(k) => Some(k),
_ => None, _ => None,
@ -738,7 +738,7 @@ impl Span {
/// Checks if a span is "internal" to a macro in which `unsafe` /// Checks if a span is "internal" to a macro in which `unsafe`
/// can be used without triggering the `unsafe_code` lint. /// can be used without triggering the `unsafe_code` lint.
// (that is, a macro marked with `#[allow_internal_unsafe]`). // (that is, a macro marked with `#[allow_internal_unsafe]`).
pub fn allows_unsafe(&self) -> bool { pub fn allows_unsafe(self) -> bool {
self.ctxt().outer_expn_data().allow_internal_unsafe self.ctxt().outer_expn_data().allow_internal_unsafe
} }
@ -751,7 +751,7 @@ impl Span {
return None; return None;
} }
let is_recursive = expn_data.call_site.source_equal(&prev_span); let is_recursive = expn_data.call_site.source_equal(prev_span);
prev_span = self; prev_span = self;
self = expn_data.call_site; self = expn_data.call_site;
@ -865,13 +865,13 @@ impl Span {
/// Equivalent of `Span::call_site` from the proc macro API, /// Equivalent of `Span::call_site` from the proc macro API,
/// except that the location is taken from the `self` span. /// except that the location is taken from the `self` span.
pub fn with_call_site_ctxt(&self, expn_id: ExpnId) -> Span { pub fn with_call_site_ctxt(self, expn_id: ExpnId) -> Span {
self.with_ctxt_from_mark(expn_id, Transparency::Transparent) self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
} }
/// Equivalent of `Span::mixed_site` from the proc macro API, /// Equivalent of `Span::mixed_site` from the proc macro API,
/// except that the location is taken from the `self` span. /// except that the location is taken from the `self` span.
pub fn with_mixed_site_ctxt(&self, expn_id: ExpnId) -> Span { pub fn with_mixed_site_ctxt(self, expn_id: ExpnId) -> Span {
self.with_ctxt_from_mark(expn_id, Transparency::SemiTransparent) self.with_ctxt_from_mark(expn_id, Transparency::SemiTransparent)
} }

View file

@ -61,6 +61,15 @@ use rustc_data_structures::fx::FxIndexSet;
/// using the callback `SPAN_TRACK` to access the query engine. /// using the callback `SPAN_TRACK` to access the query engine.
/// ///
#[derive(Clone, Copy, Eq, PartialEq, Hash)] #[derive(Clone, Copy, Eq, PartialEq, Hash)]
// FIXME(@lcnr): Enable this attribute once the bootstrap
// compiler knows of `rustc_pass_by_value`.
//
// Right now, this lint would only trigger when compiling the
// stage 2 compiler, which is fairly annoying as there are
// a lot of places using `&Span` right now. After the next bootstrap bump,
// the lint will already trigger when using stage 1, which is a lot less annoying.
//
// #[cfg_attr(not(bootstrap), rustc_pass_by_value)]
pub struct Span { pub struct Span {
base_or_index: u32, base_or_index: u32,
len_or_tag: u16, len_or_tag: u16,