Skip to content

Commit 9fc297a

Browse files
authored
Rollup merge of #99140 - TaKO8Ki:implement-is-accessible-span, r=fee1-dead
Implement `SourceMap::is_span_accessible` This patch adds `SourceMap::is_span_accessible` and replaces `span_to_snippet(span).is_ok()` and `span_to_snippet(span).is_err()` with it. This removes a `&str` to `String` conversion.
2 parents 93f71d4 + 018155c commit 9fc297a

File tree

9 files changed

+16
-16
lines changed

9 files changed

+16
-16
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
309309
));
310310

311311
// Check first whether the source is accessible (issue #87060)
312-
if self.infcx.tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
312+
if self.infcx.tcx.sess.source_map().is_span_accessible(deref_target) {
313313
err.span_note(deref_target, "deref defined here");
314314
}
315315
}

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
975975
if self.fn_self_span_reported.insert(fn_span) {
976976
err.span_note(
977977
// Check whether the source is accessible
978-
if self
979-
.infcx
980-
.tcx
981-
.sess
982-
.source_map()
983-
.span_to_snippet(self_arg.span)
984-
.is_ok()
985-
{
978+
if self.infcx.tcx.sess.source_map().is_span_accessible(self_arg.span) {
986979
self_arg.span
987980
} else {
988981
fn_call_span

Diff for: compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
299299
err.note(&format!("attempting to deref into `{}`", deref_target_ty));
300300

301301
// Check first whether the source is accessible (issue #87060)
302-
if tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
302+
if tcx.sess.source_map().is_span_accessible(deref_target) {
303303
err.span_note(deref_target, "deref defined here");
304304
}
305305

Diff for: compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
15581558
insertion_span: Span,
15591559
) {
15601560
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
1561-
if source_map.span_to_snippet(insertion_span).is_err() {
1561+
if !source_map.is_span_accessible(insertion_span) {
15621562
// Do not try to suggest anything if generated by a proc-macro.
15631563
return;
15641564
}

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
432432
"`let` bindings require an \"irrefutable pattern\", like a `struct` or \
433433
an `enum` with only one variant",
434434
);
435-
if self.tcx.sess.source_map().span_to_snippet(span).is_ok() {
435+
if self.tcx.sess.source_map().is_span_accessible(span) {
436436
let semi_span = span.shrink_to_hi().with_lo(span.hi() - BytePos(1));
437437
let start_span = span.shrink_to_lo();
438438
let end_span = semi_span.shrink_to_lo();

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ impl<'a> Resolver<'a> {
16471647

16481648
fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String {
16491649
let res = b.res();
1650-
if b.span.is_dummy() || self.session.source_map().span_to_snippet(b.span).is_err() {
1650+
if b.span.is_dummy() || !self.session.source_map().is_span_accessible(b.span) {
16511651
// These already contain the "built-in" prefix or look bad with it.
16521652
let add_built_in =
16531653
!matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod);

Diff for: compiler/rustc_span/src/source_map.rs

+7
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ impl SourceMap {
597597
local_begin.sf.src.is_some() && local_end.sf.src.is_some()
598598
}
599599

600+
pub fn is_span_accessible(&self, sp: Span) -> bool {
601+
self.span_to_source(sp, |src, start_index, end_index| {
602+
Ok(src.get(start_index..end_index).is_some())
603+
})
604+
.map_or(false, |is_accessible| is_accessible)
605+
}
606+
600607
/// Returns the source snippet as `String` corresponding to the given `Span`.
601608
pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
602609
self.span_to_source(sp, |src, start_index, end_index| {

Diff for: compiler/rustc_typeck/src/check/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
760760
if let Some(call_span) =
761761
iter::successors(Some(expr.span), |s| s.parent_callsite())
762762
.find(|&s| sp.contains(s))
763-
&& sm.span_to_snippet(call_span).is_ok()
763+
&& sm.is_span_accessible(call_span)
764764
{
765765
return Some((
766766
sp.with_hi(call_span.lo()),
@@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
773773
return None;
774774
}
775775
if sp.contains(expr.span)
776-
&& sm.span_to_snippet(expr.span).is_ok()
776+
&& sm.is_span_accessible(expr.span)
777777
{
778778
return Some((
779779
sp.with_hi(expr.span.lo()),

Diff for: compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
812812
/// Builds the `type defined here` message.
813813
fn show_definition(&self, err: &mut Diagnostic) {
814814
let mut spans: MultiSpan = if let Some(def_span) = self.tcx.def_ident_span(self.def_id) {
815-
if self.tcx.sess.source_map().span_to_snippet(def_span).is_ok() {
815+
if self.tcx.sess.source_map().is_span_accessible(def_span) {
816816
def_span.into()
817817
} else {
818818
return;

0 commit comments

Comments
 (0)