Skip to content

Commit 9c7969d

Browse files
committed
Use hygiene to access the injected crate (core or std) from builtin macros.
1 parent 8db163e commit 9c7969d

File tree

20 files changed

+99
-102
lines changed

20 files changed

+99
-102
lines changed

src/librustc_resolve/build_reduced_graph.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ impl<'a> Resolver<'a> {
262262
let module =
263263
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
264264
self.populate_module_if_necessary(module);
265+
if self.injected_crate_name.map_or(false, |name| item.ident.name == name) {
266+
self.injected_crate = Some(module);
267+
}
268+
265269
let used = self.process_legacy_macro_imports(item, module, expansion);
266270
let binding =
267271
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
@@ -561,8 +565,7 @@ impl<'a> Resolver<'a> {
561565
if let Some(id) = self.definitions.as_local_node_id(def_id) {
562566
self.local_macro_def_scopes[&id]
563567
} else if def_id.krate == BUILTIN_MACROS_CRATE {
564-
// FIXME(jseyfried): This happens when `include!()`ing a `$crate::` path, c.f, #40469.
565-
self.graph_root
568+
self.injected_crate.unwrap_or(self.graph_root)
566569
} else {
567570
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
568571
self.get_module(module_def_id)

src/librustc_resolve/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
5858
use syntax::ast::{Local, Mutability, Pat, PatKind, Path};
5959
use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6060
use syntax::feature_gate::{feature_err, emit_feature_err, GateIssue};
61+
use syntax::std_inject::injected_crate_name;
6162

6263
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6364
use errors::{DiagnosticBuilder, DiagnosticId};
@@ -1333,6 +1334,9 @@ pub struct Resolver<'a> {
13331334

13341335
// Only used for better errors on `fn(): fn()`
13351336
current_type_ascription: Vec<Span>,
1337+
1338+
injected_crate_name: Option<&'static str>,
1339+
injected_crate: Option<Module<'a>>,
13361340
}
13371341

13381342
pub struct ResolverArenas<'a> {
@@ -1532,6 +1536,8 @@ impl<'a> Resolver<'a> {
15321536
found_unresolved_macro: false,
15331537
unused_macros: FxHashSet(),
15341538
current_type_ascription: Vec::new(),
1539+
injected_crate_name: injected_crate_name(krate),
1540+
injected_crate: None,
15351541
}
15361542
}
15371543

src/libsyntax/ext/base.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ use fold::{self, Folder};
2121
use parse::{self, parser, DirectoryOwnership};
2222
use parse::token;
2323
use ptr::P;
24-
use symbol::Symbol;
24+
use symbol::{keywords, Ident, Symbol};
2525
use util::small_vector::SmallVector;
2626

2727
use std::collections::HashMap;
28+
use std::iter;
2829
use std::path::PathBuf;
2930
use std::rc::Rc;
3031
use std::default::Default;
@@ -664,7 +665,6 @@ pub struct ExpansionData {
664665
pub struct ExtCtxt<'a> {
665666
pub parse_sess: &'a parse::ParseSess,
666667
pub ecfg: expand::ExpansionConfig<'a>,
667-
pub crate_root: Option<&'static str>,
668668
pub root_path: PathBuf,
669669
pub resolver: &'a mut Resolver,
670670
pub resolve_err_count: usize,
@@ -680,7 +680,6 @@ impl<'a> ExtCtxt<'a> {
680680
ExtCtxt {
681681
parse_sess,
682682
ecfg,
683-
crate_root: None,
684683
root_path: PathBuf::new(),
685684
resolver,
686685
resolve_err_count: 0,
@@ -822,12 +821,10 @@ impl<'a> ExtCtxt<'a> {
822821
ast::Ident::from_str(st)
823822
}
824823
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
825-
let mut v = Vec::new();
826-
if let Some(s) = self.crate_root {
827-
v.push(self.ident_of(s));
828-
}
829-
v.extend(components.iter().map(|s| self.ident_of(s)));
830-
v
824+
let def_site = SyntaxContext::empty().apply_mark(self.current_expansion.mark);
825+
iter::once(Ident { ctxt: def_site, ..keywords::DollarCrate.ident() })
826+
.chain(components.iter().map(|s| self.ident_of(s)))
827+
.collect()
831828
}
832829
pub fn name_of(&self, st: &str) -> ast::Name {
833830
Symbol::intern(st)

src/libsyntax/ext/build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
319319
types: Vec<P<ast::Ty>>,
320320
bindings: Vec<ast::TypeBinding> )
321321
-> ast::Path {
322+
use syntax::parse::token;
323+
322324
let last_identifier = idents.pop().unwrap();
323325
let mut segments: Vec<ast::PathSegment> = Vec::new();
324-
if global {
326+
if global &&
327+
!idents.first().map_or(false, |&ident| token::Ident(ident).is_path_segment_keyword()) {
325328
segments.push(ast::PathSegment::crate_root(span));
326329
}
327330

src/libsyntax/ext/expand.rs

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use parse::{DirectoryOwnership, PResult};
2525
use parse::token::{self, Token};
2626
use parse::parser::Parser;
2727
use ptr::P;
28-
use std_inject;
2928
use symbol::Symbol;
3029
use symbol::keywords;
3130
use syntax_pos::{Span, DUMMY_SP};
@@ -219,7 +218,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
219218
}
220219

221220
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
222-
self.cx.crate_root = std_inject::injected_crate_name(&krate);
223221
let mut module = ModuleData {
224222
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
225223
directory: self.cx.codemap().span_to_unmapped_path(krate.span),

src/libsyntax/test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn generate_test_harness(sess: &ParseSess,
272272

273273
let mark = Mark::fresh(Mark::root());
274274

275-
let mut cx: TestCtxt = TestCtxt {
275+
let cx = TestCtxt {
276276
span_diagnostic: sd,
277277
ext_cx: ExtCtxt::new(sess, ExpansionConfig::default("test".to_string()), resolver),
278278
path: Vec::new(),
@@ -283,7 +283,6 @@ fn generate_test_harness(sess: &ParseSess,
283283
toplevel_reexport: None,
284284
ctxt: SyntaxContext::empty().apply_mark(mark),
285285
};
286-
cx.ext_cx.crate_root = Some("std");
287286

288287
mark.set_expn_info(ExpnInfo {
289288
call_site: DUMMY_SP,

src/libsyntax_ext/deriving/bounds.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
13-
1414
use syntax::ast::MetaItem;
1515
use syntax::ext::base::{Annotatable, ExtCtxt};
1616
use syntax_pos::Span;
@@ -28,15 +28,10 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt,
2828
mitem: &MetaItem,
2929
item: &Annotatable,
3030
push: &mut FnMut(Annotatable)) {
31-
let mut v = cx.crate_root.map(|s| vec![s]).unwrap_or(Vec::new());
32-
v.push("marker");
33-
v.push("Copy");
34-
let path = Path::new(v);
35-
3631
let trait_def = TraitDef {
3732
span,
3833
attributes: Vec::new(),
39-
path,
34+
path: path_std!(cx, marker::Copy),
4035
additional_bounds: Vec::new(),
4136
generics: LifetimeBounds::empty(),
4237
is_unsafe: false,

src/libsyntax_ext/deriving/clone.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -55,7 +56,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
5556
}));
5657
}
5758
ItemKind::Union(..) => {
58-
bounds = vec![Literal(path_std!(cx, core::marker::Copy))];
59+
bounds = vec![Literal(path_std!(cx, marker::Copy))];
5960
is_shallow = true;
6061
substructure = combine_substructure(Box::new(|c, s, sub| {
6162
cs_clone_shallow("Clone", c, s, sub, true)
@@ -79,7 +80,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
7980
let trait_def = TraitDef {
8081
span,
8182
attributes: Vec::new(),
82-
path: path_std!(cx, core::clone::Clone),
83+
path: path_std!(cx, clone::Clone),
8384
additional_bounds: bounds,
8485
generics: LifetimeBounds::empty(),
8586
is_unsafe: false,

src/libsyntax_ext/deriving/cmp/eq.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -30,7 +31,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
3031
let trait_def = TraitDef {
3132
span,
3233
attributes: Vec::new(),
33-
path: path_std!(cx, core::cmp::Eq),
34+
path: path_std!(cx, cmp::Eq),
3435
additional_bounds: Vec::new(),
3536
generics: LifetimeBounds::empty(),
3637
is_unsafe: false,

src/libsyntax_ext/deriving/cmp/ord.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -28,7 +29,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
2829
let trait_def = TraitDef {
2930
span,
3031
attributes: Vec::new(),
31-
path: path_std!(cx, core::cmp::Ord),
32+
path: path_std!(cx, cmp::Ord),
3233
additional_bounds: Vec::new(),
3334
generics: LifetimeBounds::empty(),
3435
is_unsafe: false,
@@ -38,7 +39,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
3839
generics: LifetimeBounds::empty(),
3940
explicit_self: borrowed_explicit_self(),
4041
args: vec![borrowed_self()],
41-
ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
42+
ret_ty: Literal(path_std!(cx, cmp::Ordering)),
4243
attributes: attrs,
4344
is_unsafe: false,
4445
unify_fieldless_variants: true,

src/libsyntax_ext/deriving/cmp/partial_eq.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::{path_local, path_std};
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -93,7 +94,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
9394
let trait_def = TraitDef {
9495
span,
9596
attributes: Vec::new(),
96-
path: path_std!(cx, core::cmp::PartialEq),
97+
path: path_std!(cx, cmp::PartialEq),
9798
additional_bounds: Vec::new(),
9899
generics: LifetimeBounds::empty(),
99100
is_unsafe: false,

src/libsyntax_ext/deriving/cmp/partial_ord.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
pub use self::OrderingOp::*;
1212

13+
use deriving::{path_local, pathvec_std, path_std};
1314
use deriving::generic::*;
1415
use deriving::generic::ty::*;
1516

@@ -45,11 +46,11 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
4546
} }
4647
}
4748

48-
let ordering_ty = Literal(path_std!(cx, core::cmp::Ordering));
49-
let ret_ty = Literal(Path::new_(pathvec_std!(cx, core::option::Option),
49+
let ordering_ty = Literal(path_std!(cx, cmp::Ordering));
50+
let ret_ty = Literal(Path::new_(pathvec_std!(cx, option::Option),
5051
None,
5152
vec![Box::new(ordering_ty)],
52-
true));
53+
PathKind::Std));
5354

5455
let inline = cx.meta_word(span, Symbol::intern("inline"));
5556
let attrs = vec![cx.attribute(span, inline)];
@@ -84,7 +85,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
8485
let trait_def = TraitDef {
8586
span,
8687
attributes: vec![],
87-
path: path_std!(cx, core::cmp::PartialOrd),
88+
path: path_std!(cx, cmp::PartialOrd),
8889
additional_bounds: vec![],
8990
generics: LifetimeBounds::empty(),
9091
is_unsafe: false,

src/libsyntax_ext/deriving/debug.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -24,13 +25,13 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt,
2425
item: &Annotatable,
2526
push: &mut FnMut(Annotatable)) {
2627
// &mut ::std::fmt::Formatter
27-
let fmtr = Ptr(Box::new(Literal(path_std!(cx, core::fmt::Formatter))),
28+
let fmtr = Ptr(Box::new(Literal(path_std!(cx, fmt::Formatter))),
2829
Borrowed(None, ast::Mutability::Mutable));
2930

3031
let trait_def = TraitDef {
3132
span,
3233
attributes: Vec::new(),
33-
path: path_std!(cx, core::fmt::Debug),
34+
path: path_std!(cx, fmt::Debug),
3435
additional_bounds: Vec::new(),
3536
generics: LifetimeBounds::empty(),
3637
is_unsafe: false,
@@ -40,7 +41,7 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt,
4041
generics: LifetimeBounds::empty(),
4142
explicit_self: borrowed_explicit_self(),
4243
args: vec![fmtr],
43-
ret_ty: Literal(path_std!(cx, core::fmt::Result)),
44+
ret_ty: Literal(path_std!(cx, fmt::Result)),
4445
attributes: Vec::new(),
4546
is_unsafe: false,
4647
unify_fieldless_variants: false,

src/libsyntax_ext/deriving/decodable.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! The compiler code necessary for `#[derive(Decodable)]`. See encodable.rs for more.
1212
13-
use deriving;
13+
use deriving::{self, pathvec_std};
1414
use deriving::generic::*;
1515
use deriving::generic::ty::*;
1616
use deriving::warn_if_deprecated;
@@ -46,20 +46,12 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
4646
item: &Annotatable,
4747
push: &mut FnMut(Annotatable),
4848
krate: &'static str) {
49-
if cx.crate_root != Some("std") {
50-
// FIXME(#21880): lift this requirement.
51-
cx.span_err(span,
52-
"this trait cannot be derived with #![no_std] \
53-
or #![no_core]");
54-
return;
55-
}
56-
5749
let typaram = &*deriving::hygienic_type_parameter(item, "__D");
5850

5951
let trait_def = TraitDef {
6052
span,
6153
attributes: Vec::new(),
62-
path: Path::new_(vec![krate, "Decodable"], None, vec![], true),
54+
path: Path::new_(vec![krate, "Decodable"], None, vec![], PathKind::Global),
6355
additional_bounds: Vec::new(),
6456
generics: LifetimeBounds::empty(),
6557
is_unsafe: false,
@@ -72,18 +64,18 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
7264
vec![Path::new_(vec![krate, "Decoder"],
7365
None,
7466
vec![],
75-
true)])],
67+
PathKind::Global)])],
7668
},
7769
explicit_self: None,
7870
args: vec![Ptr(Box::new(Literal(Path::new_local(typaram))),
7971
Borrowed(None, Mutability::Mutable))],
8072
ret_ty:
81-
Literal(Path::new_(pathvec_std!(cx, core::result::Result),
73+
Literal(Path::new_(pathvec_std!(cx, result::Result),
8274
None,
8375
vec![Box::new(Self_), Box::new(Literal(Path::new_(
84-
vec![typaram, "Error"], None, vec![], false
76+
vec![typaram, "Error"], None, vec![], PathKind::Local
8577
)))],
86-
true)),
78+
PathKind::Std)),
8779
attributes: Vec::new(),
8880
is_unsafe: false,
8981
unify_fieldless_variants: false,

src/libsyntax_ext/deriving/default.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use deriving::path_std;
1112
use deriving::generic::*;
1213
use deriving::generic::ty::*;
1314

@@ -28,7 +29,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
2829
let trait_def = TraitDef {
2930
span,
3031
attributes: Vec::new(),
31-
path: path_std!(cx, core::default::Default),
32+
path: path_std!(cx, default::Default),
3233
additional_bounds: Vec::new(),
3334
generics: LifetimeBounds::empty(),
3435
is_unsafe: false,

0 commit comments

Comments
 (0)