summary refs log tree commit diff
path: root/compiler/rustc_span/src
diff options
context:
space:
mode:
authorEsteban Kuber <esteban@kuber.com.ar>2021-11-20 18:46:36 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2021-12-03 18:41:40 +0000
commit962b2197a5d5796ee030a05f534ee0fde8ce07bb (patch)
treeb9cec8cf9bceb90e7ad55c5f669eee43a29da822 /compiler/rustc_span/src
parent8bee2b88c075a2f007a7d5762727a840477c0893 (diff)
downloadrust-962b2197a5d5796ee030a05f534ee0fde8ce07bb.tar.gz
rust-962b2197a5d5796ee030a05f534ee0fde8ce07bb.zip
Annotate `derive`d spans and move span suggestion code
* Annotate `derive`d spans from the user's code with the appropciate context
* Add `Span::can_be_used_for_suggestion` to query if the underlying span
  at the users' code
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/lib.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index 66c01140abc..98478cf5dec 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -551,6 +551,16 @@ impl Span {
         matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
     }
 
+    /// Gate suggestions that would not be appropriate in a context the user didn't write.
+    pub fn can_be_used_for_suggestions(self) -> bool {
+        !self.from_expansion()
+        // FIXME: If this span comes from a `derive` macro but it points at code the user wrote,
+        // the callsite span and the span will be pointing at different places. It also means that
+        // we can safely provide suggestions on this span.
+            || (matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
+                && self.parent_callsite().map(|p| (p.lo(), p.hi())) != Some((self.lo(), self.hi())))
+    }
+
     #[inline]
     pub fn with_root_ctxt(lo: BytePos, hi: BytePos) -> Span {
         Span::new(lo, hi, SyntaxContext::root(), None)