about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-06-19 19:26:26 +0200
committerGitHub <noreply@github.com>2023-06-19 19:26:26 +0200
commitaf348a81a073646741feff64f5082080aa0e6fd0 (patch)
tree2a5899c784aa3c26ef03863bfa9b6204864d3040
parent663fb5a0e9c02279bd063b06235e4e8e4bd9fb50 (diff)
parentfe4aec1c4da0e92dbb727ebe5a2ee8951a6a45d9 (diff)
downloadrust-af348a81a073646741feff64f5082080aa0e6fd0.tar.gz
rust-af348a81a073646741feff64f5082080aa0e6fd0.zip
Rollup merge of #112705 - WaffleLapkin:simplify_source_callee_impl, r=cjgillot
Simplify `Span::source_callee` impl

Imo the iterator impl is easier to grasp.
-rw-r--r--compiler/rustc_span/src/lib.rs17
1 files changed, 10 insertions, 7 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index e7a53c63e83..3bb9c4920c4 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -65,11 +65,11 @@ use rustc_data_structures::sync::{Lock, Lrc};
 
 use std::borrow::Cow;
 use std::cmp::{self, Ordering};
-use std::fmt;
 use std::hash::Hash;
 use std::ops::{Add, Range, Sub};
 use std::path::{Path, PathBuf};
 use std::str::FromStr;
+use std::{fmt, iter};
 
 use md5::Digest;
 use md5::Md5;
@@ -733,12 +733,15 @@ impl Span {
     /// else returns the `ExpnData` for the macro definition
     /// corresponding to the source callsite.
     pub fn source_callee(self) -> Option<ExpnData> {
-        fn source_callee(expn_data: ExpnData) -> ExpnData {
-            let next_expn_data = expn_data.call_site.ctxt().outer_expn_data();
-            if !next_expn_data.is_root() { source_callee(next_expn_data) } else { expn_data }
-        }
         let expn_data = self.ctxt().outer_expn_data();
-        if !expn_data.is_root() { Some(source_callee(expn_data)) } else { None }
+
+        // Create an iterator of call site expansions
+        iter::successors(Some(expn_data), |expn_data| {
+            Some(expn_data.call_site.ctxt().outer_expn_data())
+        })
+        // Find the last expansion which is not root
+        .take_while(|expn_data| !expn_data.is_root())
+        .last()
     }
 
     /// Checks if a span is "internal" to a macro in which `#[unstable]`
@@ -777,7 +780,7 @@ impl Span {
 
     pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
         let mut prev_span = DUMMY_SP;
-        std::iter::from_fn(move || {
+        iter::from_fn(move || {
             loop {
                 let expn_data = self.ctxt().outer_expn_data();
                 if expn_data.is_root() {