From 12d11e9a35ccb380388c0568ed1d8735d89f51c3 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 11 Jul 2022 11:36:15 +0900 Subject: [PATCH 1/2] implement `is_accessible_span` --- .../rustc_borrowck/src/diagnostics/conflict_errors.rs | 2 +- compiler/rustc_borrowck/src/diagnostics/mod.rs | 9 +-------- .../rustc_const_eval/src/transform/check_consts/ops.rs | 2 +- compiler/rustc_errors/src/lib.rs | 2 +- compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 2 +- compiler/rustc_resolve/src/diagnostics.rs | 2 +- compiler/rustc_span/src/source_map.rs | 7 +++++++ compiler/rustc_typeck/src/check/demand.rs | 4 ++-- .../structured_errors/wrong_number_of_generic_args.rs | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index b9cfc3732dc7c..ed628f35148ef 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -309,7 +309,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { )); // Check first whether the source is accessible (issue #87060) - if self.infcx.tcx.sess.source_map().span_to_snippet(deref_target).is_ok() { + if self.infcx.tcx.sess.source_map().is_accessible_span(deref_target) { err.span_note(deref_target, "deref defined here"); } } diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 6fea6941085ce..3ecf259935a59 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -975,14 +975,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if self.fn_self_span_reported.insert(fn_span) { err.span_note( // Check whether the source is accessible - if self - .infcx - .tcx - .sess - .source_map() - .span_to_snippet(self_arg.span) - .is_ok() - { + if self.infcx.tcx.sess.source_map().is_accessible_span(self_arg.span) { self_arg.span } else { fn_call_span diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index 17376e59e09cc..631a646b2bcb7 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -299,7 +299,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { err.note(&format!("attempting to deref into `{}`", deref_target_ty)); // Check first whether the source is accessible (issue #87060) - if tcx.sess.source_map().span_to_snippet(deref_target).is_ok() { + if tcx.sess.source_map().is_accessible_span(deref_target) { err.span_note(deref_target, "deref defined here"); } diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index ffe4ecebb2e36..f4624c0763603 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1558,7 +1558,7 @@ pub fn add_elided_lifetime_in_path_suggestion( insertion_span: Span, ) { diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n))); - if source_map.span_to_snippet(insertion_span).is_err() { + if !source_map.is_accessible_span(insertion_span) { // Do not try to suggest anything if generated by a proc-macro. return; } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 61ac3d14e50e2..f3d0002563f0e 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -432,7 +432,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { "`let` bindings require an \"irrefutable pattern\", like a `struct` or \ an `enum` with only one variant", ); - if self.tcx.sess.source_map().span_to_snippet(span).is_ok() { + if self.tcx.sess.source_map().is_accessible_span(span) { let semi_span = span.shrink_to_hi().with_lo(span.hi() - BytePos(1)); let start_span = span.shrink_to_lo(); let end_span = semi_span.shrink_to_lo(); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 18ffe9528f565..eb2de140441db 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1650,7 +1650,7 @@ impl<'a> Resolver<'a> { fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String { let res = b.res(); - if b.span.is_dummy() || self.session.source_map().span_to_snippet(b.span).is_err() { + if b.span.is_dummy() || !self.session.source_map().is_accessible_span(b.span) { // These already contain the "built-in" prefix or look bad with it. let add_built_in = !matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod); diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 227127aed50d7..66ed32dced69e 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -597,6 +597,13 @@ impl SourceMap { local_begin.sf.src.is_some() && local_end.sf.src.is_some() } + pub fn is_accessible_span(&self, sp: Span) -> bool { + self.span_to_source(sp, |src, start_index, end_index| { + Ok(src.get(start_index..end_index).is_some()) + }) + .map_or(false, |is_accessible| is_accessible) + } + /// Returns the source snippet as `String` corresponding to the given `Span`. pub fn span_to_snippet(&self, sp: Span) -> Result { self.span_to_source(sp, |src, start_index, end_index| { diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 53ca027bb57f5..af30526b35a7e 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -760,7 +760,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(call_span) = iter::successors(Some(expr.span), |s| s.parent_callsite()) .find(|&s| sp.contains(s)) - && sm.span_to_snippet(call_span).is_ok() + && sm.is_accessible_span(call_span) { return Some(( sp.with_hi(call_span.lo()), @@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return None; } if sp.contains(expr.span) - && sm.span_to_snippet(expr.span).is_ok() + && sm.is_accessible_span(expr.span) { return Some(( sp.with_hi(expr.span.lo()), diff --git a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs index 72a32dade4eef..23af72bf56271 100644 --- a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs @@ -812,7 +812,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { /// Builds the `type defined here` message. fn show_definition(&self, err: &mut Diagnostic) { let mut spans: MultiSpan = if let Some(def_span) = self.tcx.def_ident_span(self.def_id) { - if self.tcx.sess.source_map().span_to_snippet(def_span).is_ok() { + if self.tcx.sess.source_map().is_accessible_span(def_span) { def_span.into() } else { return; From 018155c3a218b819b70d1dc57b08fe3bc7b2ec3c Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 11 Jul 2022 16:51:19 +0900 Subject: [PATCH 2/2] rename a method --- compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs | 2 +- compiler/rustc_borrowck/src/diagnostics/mod.rs | 2 +- compiler/rustc_const_eval/src/transform/check_consts/ops.rs | 2 +- compiler/rustc_errors/src/lib.rs | 2 +- compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 2 +- compiler/rustc_resolve/src/diagnostics.rs | 2 +- compiler/rustc_span/src/source_map.rs | 2 +- compiler/rustc_typeck/src/check/demand.rs | 4 ++-- .../src/structured_errors/wrong_number_of_generic_args.rs | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index ed628f35148ef..687ff0fb50580 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -309,7 +309,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { )); // Check first whether the source is accessible (issue #87060) - if self.infcx.tcx.sess.source_map().is_accessible_span(deref_target) { + if self.infcx.tcx.sess.source_map().is_span_accessible(deref_target) { err.span_note(deref_target, "deref defined here"); } } diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 3ecf259935a59..a3094a34ad7f3 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -975,7 +975,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if self.fn_self_span_reported.insert(fn_span) { err.span_note( // Check whether the source is accessible - if self.infcx.tcx.sess.source_map().is_accessible_span(self_arg.span) { + if self.infcx.tcx.sess.source_map().is_span_accessible(self_arg.span) { self_arg.span } else { fn_call_span diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index 631a646b2bcb7..1d083b0bf8268 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -299,7 +299,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { err.note(&format!("attempting to deref into `{}`", deref_target_ty)); // Check first whether the source is accessible (issue #87060) - if tcx.sess.source_map().is_accessible_span(deref_target) { + if tcx.sess.source_map().is_span_accessible(deref_target) { err.span_note(deref_target, "deref defined here"); } diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index f4624c0763603..e59a74e380ae3 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1558,7 +1558,7 @@ pub fn add_elided_lifetime_in_path_suggestion( insertion_span: Span, ) { diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n))); - if !source_map.is_accessible_span(insertion_span) { + if !source_map.is_span_accessible(insertion_span) { // Do not try to suggest anything if generated by a proc-macro. return; } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index f3d0002563f0e..75fd156ebfdf3 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -432,7 +432,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { "`let` bindings require an \"irrefutable pattern\", like a `struct` or \ an `enum` with only one variant", ); - if self.tcx.sess.source_map().is_accessible_span(span) { + if self.tcx.sess.source_map().is_span_accessible(span) { let semi_span = span.shrink_to_hi().with_lo(span.hi() - BytePos(1)); let start_span = span.shrink_to_lo(); let end_span = semi_span.shrink_to_lo(); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index eb2de140441db..a75ca3a8a2e72 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1650,7 +1650,7 @@ impl<'a> Resolver<'a> { fn binding_description(&self, b: &NameBinding<'_>, ident: Ident, from_prelude: bool) -> String { let res = b.res(); - if b.span.is_dummy() || !self.session.source_map().is_accessible_span(b.span) { + if b.span.is_dummy() || !self.session.source_map().is_span_accessible(b.span) { // These already contain the "built-in" prefix or look bad with it. let add_built_in = !matches!(b.res(), Res::NonMacroAttr(..) | Res::PrimTy(..) | Res::ToolMod); diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 66ed32dced69e..afbb88e923360 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -597,7 +597,7 @@ impl SourceMap { local_begin.sf.src.is_some() && local_end.sf.src.is_some() } - pub fn is_accessible_span(&self, sp: Span) -> bool { + pub fn is_span_accessible(&self, sp: Span) -> bool { self.span_to_source(sp, |src, start_index, end_index| { Ok(src.get(start_index..end_index).is_some()) }) diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index af30526b35a7e..34876a0748fac 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -760,7 +760,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(call_span) = iter::successors(Some(expr.span), |s| s.parent_callsite()) .find(|&s| sp.contains(s)) - && sm.is_accessible_span(call_span) + && sm.is_span_accessible(call_span) { return Some(( sp.with_hi(call_span.lo()), @@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return None; } if sp.contains(expr.span) - && sm.is_accessible_span(expr.span) + && sm.is_span_accessible(expr.span) { return Some(( sp.with_hi(expr.span.lo()), diff --git a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs index 23af72bf56271..f1caa2a1ca068 100644 --- a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs @@ -812,7 +812,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { /// Builds the `type defined here` message. fn show_definition(&self, err: &mut Diagnostic) { let mut spans: MultiSpan = if let Some(def_span) = self.tcx.def_ident_span(self.def_id) { - if self.tcx.sess.source_map().is_accessible_span(def_span) { + if self.tcx.sess.source_map().is_span_accessible(def_span) { def_span.into() } else { return;