Skip to content

Commit f7a8a97

Browse files
committed
DRY std_path
1 parent 31ca232 commit f7a8a97

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/librustc/hir/lowering.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ pub trait Resolver {
151151
/// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
152152
/// This should only return `None` during testing.
153153
fn definitions(&mut self) -> &mut Definitions;
154+
155+
/// Given suffix ["b","c","d"], returns path `::cratename::b::c::d` when
156+
/// The path is also resolved according to `is_value`.
157+
fn std_path(&mut self, span: Span, crate_root: Option<&str>,
158+
components: &[&str], is_value: bool) -> hir::Path {
159+
let mut path = hir::Path {
160+
span,
161+
def: Def::Err,
162+
segments: iter::once(keywords::CrateRoot.name()).chain({
163+
crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
164+
}).map(hir::PathSegment::from_name).collect(),
165+
};
166+
167+
self.resolve_hir_path(&mut path, is_value);
168+
path
169+
}
154170
}
155171

156172
#[derive(Clone, Copy, Debug)]
@@ -3625,16 +3641,7 @@ impl<'a> LoweringContext<'a> {
36253641
/// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
36263642
/// The path is also resolved according to `is_value`.
36273643
fn std_path(&mut self, span: Span, components: &[&str], is_value: bool) -> hir::Path {
3628-
let mut path = hir::Path {
3629-
span,
3630-
def: Def::Err,
3631-
segments: iter::once(keywords::CrateRoot.name()).chain({
3632-
self.crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
3633-
}).map(hir::PathSegment::from_name).collect(),
3634-
};
3635-
3636-
self.resolver.resolve_hir_path(&mut path, is_value);
3637-
path
3644+
self.resolver.std_path(span, self.crate_root, components, is_value)
36383645
}
36393646

36403647
fn signal_block_expr(&mut self,

src/librustdoc/clean/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use syntax::attr;
2525
use syntax::codemap::Spanned;
2626
use syntax::feature_gate::UnstableFeatures;
2727
use syntax::ptr::P;
28-
use syntax::symbol::{keywords, Symbol};
28+
use syntax::symbol::keywords;
2929
use syntax_pos::{self, DUMMY_SP, Pos, FileName};
3030

3131
use rustc::middle::const_val::ConstVal;
@@ -45,7 +45,7 @@ use rustc::hir;
4545

4646
use rustc_const_math::ConstInt;
4747
use std::default::Default;
48-
use std::{mem, slice, vec, iter};
48+
use std::{mem, slice, vec};
4949
use std::iter::FromIterator;
5050
use std::rc::Rc;
5151
use std::sync::Arc;
@@ -816,16 +816,15 @@ impl Clean<Attributes> for [ast::Attribute] {
816816
continue;
817817
}
818818

819-
let mut path = hir::Path {
820-
span: DUMMY_SP,
821-
def: Def::Err,
822-
segments: iter::once(keywords::CrateRoot.name()).chain({
823-
link.split("::").skip(1).map(Symbol::intern)
824-
}).map(hir::PathSegment::from_name).collect(),
819+
let path = {
820+
// This allocation could be avoided if std_path could take an iterator;
821+
// but it can't because that would break object safety. This can still be
822+
// fixed.
823+
let components = link.split("::").skip(1).collect::<Vec<_>>();
824+
println!("{:?}", components);
825+
cx.resolver.borrow_mut().std_path(DUMMY_SP, None, &components, false)
825826
};
826827

827-
cx.resolver.borrow_mut().resolve_hir_path(&mut path, false);
828-
829828
if path.def != Def::Err {
830829
attrs.links.push((link, path.def.def_id()));
831830
}

0 commit comments

Comments
 (0)