about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2017-12-25 11:37:55 +0530
committerManish Goregaokar <manishsmail@gmail.com>2018-01-22 15:21:28 +0530
commitf7a8a97b6997fc6a8a5a22a9d1c45d93869a0cb5 (patch)
treeed6572b81d8ad5fbedb3e52483638130d55e9425
parent31ca2322a06ee5dbf91e2ea21f4354593fd8ed52 (diff)
downloadrust-f7a8a97b6997fc6a8a5a22a9d1c45d93869a0cb5.tar.gz
rust-f7a8a97b6997fc6a8a5a22a9d1c45d93869a0cb5.zip
DRY std_path
-rw-r--r--src/librustc/hir/lowering.rs27
-rw-r--r--src/librustdoc/clean/mod.rs19
2 files changed, 26 insertions, 20 deletions
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs
index 32b55a05124..b7a7fcd9872 100644
--- a/src/librustc/hir/lowering.rs
+++ b/src/librustc/hir/lowering.rs
@@ -151,6 +151,22 @@ pub trait Resolver {
     /// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
     /// This should only return `None` during testing.
     fn definitions(&mut self) -> &mut Definitions;
+
+    /// Given suffix ["b","c","d"], returns path `::cratename::b::c::d` when
+    /// The path is also resolved according to `is_value`.
+    fn std_path(&mut self, span: Span, crate_root: Option<&str>,
+                components: &[&str], is_value: bool) -> hir::Path {
+        let mut path = hir::Path {
+            span,
+            def: Def::Err,
+            segments: iter::once(keywords::CrateRoot.name()).chain({
+                crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
+            }).map(hir::PathSegment::from_name).collect(),
+        };
+
+        self.resolve_hir_path(&mut path, is_value);
+        path
+    }
 }
 
 #[derive(Clone, Copy, Debug)]
@@ -3625,16 +3641,7 @@ impl<'a> LoweringContext<'a> {
     /// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
     /// The path is also resolved according to `is_value`.
     fn std_path(&mut self, span: Span, components: &[&str], is_value: bool) -> hir::Path {
-        let mut path = hir::Path {
-            span,
-            def: Def::Err,
-            segments: iter::once(keywords::CrateRoot.name()).chain({
-                self.crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
-            }).map(hir::PathSegment::from_name).collect(),
-        };
-
-        self.resolver.resolve_hir_path(&mut path, is_value);
-        path
+        self.resolver.std_path(span, self.crate_root, components, is_value)
     }
 
     fn signal_block_expr(&mut self,
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index caa0dccba82..816b8caf23e 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -25,7 +25,7 @@ use syntax::attr;
 use syntax::codemap::Spanned;
 use syntax::feature_gate::UnstableFeatures;
 use syntax::ptr::P;
-use syntax::symbol::{keywords, Symbol};
+use syntax::symbol::keywords;
 use syntax_pos::{self, DUMMY_SP, Pos, FileName};
 
 use rustc::middle::const_val::ConstVal;
@@ -45,7 +45,7 @@ use rustc::hir;
 
 use rustc_const_math::ConstInt;
 use std::default::Default;
-use std::{mem, slice, vec, iter};
+use std::{mem, slice, vec};
 use std::iter::FromIterator;
 use std::rc::Rc;
 use std::sync::Arc;
@@ -816,16 +816,15 @@ impl Clean<Attributes> for [ast::Attribute] {
                     continue;
                 }
 
-                let mut path = hir::Path {
-                    span: DUMMY_SP,
-                    def: Def::Err,
-                    segments: iter::once(keywords::CrateRoot.name()).chain({
-                        link.split("::").skip(1).map(Symbol::intern)
-                    }).map(hir::PathSegment::from_name).collect(),
+                let path = {
+                    // This allocation could be avoided if std_path could take an iterator;
+                    // but it can't because that would break object safety. This can still be
+                    // fixed.
+                    let components = link.split("::").skip(1).collect::<Vec<_>>();
+                    println!("{:?}", components);
+                    cx.resolver.borrow_mut().std_path(DUMMY_SP, None, &components, false)
                 };
 
-                cx.resolver.borrow_mut().resolve_hir_path(&mut path, false);
-
                 if path.def != Def::Err {
                     attrs.links.push((link, path.def.def_id()));
                 }