mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
Auto merge of #64469 - matthewjasper:increase-hygiene-use, r=petrochenkov
Cleanup handling of hygiene for built-in macros This makes most identifiers generated by built-in macros use def-site hygiene, not only the ones that previously used gensyms. * `ExtCtxt::ident_of` now takes a `Span` and is preferred to `Ident::{from_str, from_str_and_span}` * Remove `Span::with_legacy_ctxt` * `assert` now uses call-site hygiene because it needs to resolve `panic` unhygienically. * `concat_idents` now uses call-site hygiene because it wouldn't be very useful with def-site hygiene. * everything else is moved to def-site hygiene r? @petrochenkov
This commit is contained in:
commit
117cdf35d4
@ -1316,7 +1316,7 @@ impl<'a> LoweringContext<'a> {
|
|||||||
ImplTraitContext::Universal(in_band_ty_params),
|
ImplTraitContext::Universal(in_band_ty_params),
|
||||||
);
|
);
|
||||||
// Set the name to `impl Bound1 + Bound2`.
|
// Set the name to `impl Bound1 + Bound2`.
|
||||||
let ident = Ident::from_str(&pprust::ty_to_string(t)).with_span_pos(span);
|
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
|
||||||
in_band_ty_params.push(hir::GenericParam {
|
in_band_ty_params.push(hir::GenericParam {
|
||||||
hir_id: self.lower_node_id(def_node_id),
|
hir_id: self.lower_node_id(def_node_id),
|
||||||
name: ParamName::Plain(ident),
|
name: ParamName::Plain(ident),
|
||||||
|
@ -618,24 +618,19 @@ impl UnusedImportBraces {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trigger the lint if the nested item is a non-self single item
|
// Trigger the lint if the nested item is a non-self single item
|
||||||
let node_ident;
|
let node_name = match items[0].0.kind {
|
||||||
match items[0].0.kind {
|
|
||||||
ast::UseTreeKind::Simple(rename, ..) => {
|
ast::UseTreeKind::Simple(rename, ..) => {
|
||||||
let orig_ident = items[0].0.prefix.segments.last().unwrap().ident;
|
let orig_ident = items[0].0.prefix.segments.last().unwrap().ident;
|
||||||
if orig_ident.name == kw::SelfLower {
|
if orig_ident.name == kw::SelfLower {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
node_ident = rename.unwrap_or(orig_ident);
|
rename.unwrap_or(orig_ident).name
|
||||||
}
|
}
|
||||||
ast::UseTreeKind::Glob => {
|
ast::UseTreeKind::Glob => Symbol::intern("*"),
|
||||||
node_ident = ast::Ident::from_str("*");
|
ast::UseTreeKind::Nested(_) => return,
|
||||||
}
|
};
|
||||||
ast::UseTreeKind::Nested(_) => {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let msg = format!("braces around {} is unnecessary", node_ident.name);
|
let msg = format!("braces around {} is unnecessary", node_name);
|
||||||
cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
|
cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -444,7 +444,8 @@ impl cstore::CStore {
|
|||||||
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
|
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
|
||||||
|
|
||||||
LoadedMacro::MacroDef(ast::Item {
|
LoadedMacro::MacroDef(ast::Item {
|
||||||
ident: ast::Ident::from_str(&name.as_str()),
|
// FIXME: cross-crate hygiene
|
||||||
|
ident: ast::Ident::with_dummy_span(name.as_symbol()),
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
span: local_span,
|
span: local_span,
|
||||||
attrs: attrs.iter().cloned().collect(),
|
attrs: attrs.iter().cloned().collect(),
|
||||||
|
@ -40,7 +40,7 @@ use rustc_metadata::cstore::CStore;
|
|||||||
use syntax::ext::hygiene::{ExpnId, Transparency, SyntaxContext};
|
use syntax::ext::hygiene::{ExpnId, Transparency, SyntaxContext};
|
||||||
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
|
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
|
||||||
use syntax::ext::base::{SyntaxExtension, MacroKind, SpecialDerives};
|
use syntax::ext::base::{SyntaxExtension, MacroKind, SpecialDerives};
|
||||||
use syntax::symbol::{Symbol, kw, sym};
|
use syntax::symbol::{kw, sym};
|
||||||
|
|
||||||
use syntax::visit::{self, Visitor};
|
use syntax::visit::{self, Visitor};
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
@ -241,7 +241,7 @@ impl Segment {
|
|||||||
|
|
||||||
fn names_to_string(segments: &[Segment]) -> String {
|
fn names_to_string(segments: &[Segment]) -> String {
|
||||||
names_to_string(&segments.iter()
|
names_to_string(&segments.iter()
|
||||||
.map(|seg| seg.ident)
|
.map(|seg| seg.ident.name)
|
||||||
.collect::<Vec<_>>())
|
.collect::<Vec<_>>())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -951,7 +951,7 @@ pub struct Resolver<'a> {
|
|||||||
struct_constructors: DefIdMap<(Res, ty::Visibility)>,
|
struct_constructors: DefIdMap<(Res, ty::Visibility)>,
|
||||||
|
|
||||||
/// Features enabled for this crate.
|
/// Features enabled for this crate.
|
||||||
active_features: FxHashSet<Symbol>,
|
active_features: FxHashSet<Name>,
|
||||||
|
|
||||||
/// Stores enum visibilities to properly build a reduced graph
|
/// Stores enum visibilities to properly build a reduced graph
|
||||||
/// when visiting the correspondent variants.
|
/// when visiting the correspondent variants.
|
||||||
@ -1018,8 +1018,8 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
|
|||||||
fn resolve_str_path(
|
fn resolve_str_path(
|
||||||
&mut self,
|
&mut self,
|
||||||
span: Span,
|
span: Span,
|
||||||
crate_root: Option<Symbol>,
|
crate_root: Option<Name>,
|
||||||
components: &[Symbol],
|
components: &[Name],
|
||||||
ns: Namespace,
|
ns: Namespace,
|
||||||
) -> (ast::Path, Res) {
|
) -> (ast::Path, Res) {
|
||||||
let root = if crate_root.is_some() {
|
let root = if crate_root.is_some() {
|
||||||
@ -2555,7 +2555,7 @@ impl<'a> Resolver<'a> {
|
|||||||
fn add_suggestion_for_rename_of_use(
|
fn add_suggestion_for_rename_of_use(
|
||||||
&self,
|
&self,
|
||||||
err: &mut DiagnosticBuilder<'_>,
|
err: &mut DiagnosticBuilder<'_>,
|
||||||
name: Symbol,
|
name: Name,
|
||||||
directive: &ImportDirective<'_>,
|
directive: &ImportDirective<'_>,
|
||||||
binding_span: Span,
|
binding_span: Span,
|
||||||
) {
|
) {
|
||||||
@ -2770,22 +2770,22 @@ impl<'a> Resolver<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn names_to_string(idents: &[Ident]) -> String {
|
fn names_to_string(names: &[Name]) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
for (i, ident) in idents.iter()
|
for (i, name) in names.iter()
|
||||||
.filter(|ident| ident.name != kw::PathRoot)
|
.filter(|name| **name != kw::PathRoot)
|
||||||
.enumerate() {
|
.enumerate() {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
result.push_str("::");
|
result.push_str("::");
|
||||||
}
|
}
|
||||||
result.push_str(&ident.as_str());
|
result.push_str(&name.as_str());
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_names_to_string(path: &Path) -> String {
|
fn path_names_to_string(path: &Path) -> String {
|
||||||
names_to_string(&path.segments.iter()
|
names_to_string(&path.segments.iter()
|
||||||
.map(|seg| seg.ident)
|
.map(|seg| seg.ident.name)
|
||||||
.collect::<Vec<_>>())
|
.collect::<Vec<_>>())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2793,15 +2793,14 @@ fn path_names_to_string(path: &Path) -> String {
|
|||||||
fn module_to_string(module: Module<'_>) -> Option<String> {
|
fn module_to_string(module: Module<'_>) -> Option<String> {
|
||||||
let mut names = Vec::new();
|
let mut names = Vec::new();
|
||||||
|
|
||||||
fn collect_mod(names: &mut Vec<Ident>, module: Module<'_>) {
|
fn collect_mod(names: &mut Vec<Name>, module: Module<'_>) {
|
||||||
if let ModuleKind::Def(.., name) = module.kind {
|
if let ModuleKind::Def(.., name) = module.kind {
|
||||||
if let Some(parent) = module.parent {
|
if let Some(parent) = module.parent {
|
||||||
names.push(Ident::with_dummy_span(name));
|
names.push(name);
|
||||||
collect_mod(names, parent);
|
collect_mod(names, parent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// danger, shouldn't be ident?
|
names.push(Name::intern("<opaque>"));
|
||||||
names.push(Ident::from_str("<opaque>"));
|
|
||||||
collect_mod(names, module.parent.unwrap());
|
collect_mod(names, module.parent.unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2810,9 +2809,8 @@ fn module_to_string(module: Module<'_>) -> Option<String> {
|
|||||||
if names.is_empty() {
|
if names.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some(names_to_string(&names.into_iter()
|
names.reverse();
|
||||||
.rev()
|
Some(names_to_string(&names))
|
||||||
.collect::<Vec<_>>()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
@ -1433,15 +1433,17 @@ fn import_path_to_string(names: &[Ident],
|
|||||||
let global = !names.is_empty() && names[0].name == kw::PathRoot;
|
let global = !names.is_empty() && names[0].name == kw::PathRoot;
|
||||||
if let Some(pos) = pos {
|
if let Some(pos) = pos {
|
||||||
let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
|
let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
|
||||||
names_to_string(names)
|
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>())
|
||||||
} else {
|
} else {
|
||||||
let names = if global { &names[1..] } else { names };
|
let names = if global { &names[1..] } else { names };
|
||||||
if names.is_empty() {
|
if names.is_empty() {
|
||||||
import_directive_subclass_to_string(subclass)
|
import_directive_subclass_to_string(subclass)
|
||||||
} else {
|
} else {
|
||||||
format!("{}::{}",
|
format!(
|
||||||
names_to_string(names),
|
"{}::{}",
|
||||||
import_directive_subclass_to_string(subclass))
|
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>()),
|
||||||
|
import_directive_subclass_to_string(subclass),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -977,13 +977,6 @@ impl<'a> ExtCtxt<'a> {
|
|||||||
span.with_call_site_ctxt(self.current_expansion.id)
|
span.with_call_site_ctxt(self.current_expansion.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Span with a context reproducing `macro_rules` hygiene (hygienic locals, unhygienic items).
|
|
||||||
/// FIXME: This should be eventually replaced either with `with_def_site_ctxt` (preferably),
|
|
||||||
/// or with `with_call_site_ctxt` (where necessary).
|
|
||||||
pub fn with_legacy_ctxt(&self, span: Span) -> Span {
|
|
||||||
span.with_legacy_ctxt(self.current_expansion.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns span for the macro which originally caused the current expansion to happen.
|
/// Returns span for the macro which originally caused the current expansion to happen.
|
||||||
///
|
///
|
||||||
/// Stops backtracing at include! boundary.
|
/// Stops backtracing at include! boundary.
|
||||||
@ -1081,8 +1074,8 @@ impl<'a> ExtCtxt<'a> {
|
|||||||
pub fn set_trace_macros(&mut self, x: bool) {
|
pub fn set_trace_macros(&mut self, x: bool) {
|
||||||
self.ecfg.trace_mac = x
|
self.ecfg.trace_mac = x
|
||||||
}
|
}
|
||||||
pub fn ident_of(&self, st: &str) -> ast::Ident {
|
pub fn ident_of(&self, st: &str, sp: Span) -> ast::Ident {
|
||||||
ast::Ident::from_str(st)
|
ast::Ident::from_str_and_span(st, sp)
|
||||||
}
|
}
|
||||||
pub fn std_path(&self, components: &[Symbol]) -> Vec<ast::Ident> {
|
pub fn std_path(&self, components: &[Symbol]) -> Vec<ast::Ident> {
|
||||||
let def_site = self.with_def_site_ctxt(DUMMY_SP);
|
let def_site = self.with_def_site_ctxt(DUMMY_SP);
|
||||||
|
@ -363,7 +363,7 @@ impl<'a> ExtCtxt<'a> {
|
|||||||
self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
|
self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
|
||||||
}
|
}
|
||||||
pub fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
|
pub fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
|
||||||
let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
|
let ident = Ident::new(sym::integer(idx), sp);
|
||||||
self.expr(sp, ast::ExprKind::Field(expr, ident))
|
self.expr(sp, ast::ExprKind::Field(expr, ident))
|
||||||
}
|
}
|
||||||
pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
||||||
@ -525,7 +525,7 @@ impl<'a> ExtCtxt<'a> {
|
|||||||
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
||||||
let err_path = self.path_global(sp, err);
|
let err_path = self.path_global(sp, err);
|
||||||
|
|
||||||
let binding_variable = self.ident_of("__try_var");
|
let binding_variable = self.ident_of("__try_var", sp);
|
||||||
let binding_pat = self.pat_ident(sp, binding_variable);
|
let binding_pat = self.pat_ident(sp, binding_variable);
|
||||||
let binding_expr = self.expr_ident(sp, binding_variable);
|
let binding_expr = self.expr_ident(sp, binding_variable);
|
||||||
|
|
||||||
|
@ -1256,7 +1256,7 @@ impl<'a> Parser<'a> {
|
|||||||
for part in idents {
|
for part in idents {
|
||||||
fixed_name.push_str(&format!("_{}", part.name));
|
fixed_name.push_str(&format!("_{}", part.name));
|
||||||
}
|
}
|
||||||
ident = Ident::from_str(&fixed_name).with_span_pos(fixed_name_sp);
|
ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
|
||||||
|
|
||||||
self.struct_span_err(fixed_name_sp, error_msg)
|
self.struct_span_err(fixed_name_sp, error_msg)
|
||||||
.span_label(fixed_name_sp, "dash-separated idents are not valid")
|
.span_label(fixed_name_sp, "dash-separated idents are not valid")
|
||||||
|
@ -62,7 +62,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
|||||||
MacEager::expr(P(ast::Expr {
|
MacEager::expr(P(ast::Expr {
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
node: ast::ExprKind::InlineAsm(P(inline_asm)),
|
node: ast::ExprKind::InlineAsm(P(inline_asm)),
|
||||||
span: cx.with_legacy_ctxt(sp),
|
span: cx.with_def_site_ctxt(sp),
|
||||||
attrs: ThinVec::new(),
|
attrs: ThinVec::new(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,9 @@ pub fn expand_assert<'cx>(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let sp = cx.with_legacy_ctxt(sp);
|
// `core::panic` and `std::panic` are different macros, so we use call-site
|
||||||
|
// context to pick up whichever is currently in scope.
|
||||||
|
let sp = cx.with_call_site_ctxt(sp);
|
||||||
let panic_call = Mac {
|
let panic_call = Mac {
|
||||||
path: Path::from_ident(Ident::new(sym::panic, sp)),
|
path: Path::from_ident(Ident::new(sym::panic, sp)),
|
||||||
tts: custom_message.unwrap_or_else(|| {
|
tts: custom_message.unwrap_or_else(|| {
|
||||||
|
@ -16,7 +16,7 @@ pub fn expand_cfg(
|
|||||||
sp: Span,
|
sp: Span,
|
||||||
tts: TokenStream,
|
tts: TokenStream,
|
||||||
) -> Box<dyn base::MacResult + 'static> {
|
) -> Box<dyn base::MacResult + 'static> {
|
||||||
let sp = cx.with_legacy_ctxt(sp);
|
let sp = cx.with_def_site_ctxt(sp);
|
||||||
|
|
||||||
match parse_cfg(cx, sp, tts) {
|
match parse_cfg(cx, sp, tts) {
|
||||||
Ok(cfg) => {
|
Ok(cfg) => {
|
||||||
|
@ -59,6 +59,6 @@ pub fn expand_concat(
|
|||||||
} else if has_errors {
|
} else if has_errors {
|
||||||
return DummyResult::any(sp);
|
return DummyResult::any(sp);
|
||||||
}
|
}
|
||||||
let sp = cx.with_legacy_ctxt(sp);
|
let sp = cx.with_def_site_ctxt(sp);
|
||||||
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ pub fn expand_concat_idents<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let ident = ast::Ident::new(Symbol::intern(&res_str), cx.with_legacy_ctxt(sp));
|
let ident = ast::Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
|
||||||
|
|
||||||
struct ConcatIdentsResult { ident: ast::Ident }
|
struct ConcatIdentsResult { ident: ast::Ident }
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ pub fn some_ordering_collapsed(
|
|||||||
GtOp => "gt",
|
GtOp => "gt",
|
||||||
GeOp => "ge",
|
GeOp => "ge",
|
||||||
};
|
};
|
||||||
cx.expr_method_call(span, lft, ast::Ident::from_str_and_span(op_str, span), vec![rgt])
|
cx.expr_method_call(span, lft, cx.ident_of(op_str, span), vec![rgt])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
|
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
|
||||||
|
@ -62,7 +62,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
|
|||||||
// We want to make sure we have the ctxt set so that we can use unstable methods
|
// We want to make sure we have the ctxt set so that we can use unstable methods
|
||||||
let span = cx.with_def_site_ctxt(span);
|
let span = cx.with_def_site_ctxt(span);
|
||||||
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
|
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
|
||||||
let builder = Ident::from_str_and_span("debug_trait_builder", span);
|
let builder = cx.ident_of("debug_trait_builder", span);
|
||||||
let builder_expr = cx.expr_ident(span, builder.clone());
|
let builder_expr = cx.expr_ident(span, builder.clone());
|
||||||
|
|
||||||
let fmt = substr.nonself_args[0].clone();
|
let fmt = substr.nonself_args[0].clone();
|
||||||
@ -72,7 +72,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
|
|||||||
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
|
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
|
||||||
// tuple struct/"normal" variant
|
// tuple struct/"normal" variant
|
||||||
let expr =
|
let expr =
|
||||||
cx.expr_method_call(span, fmt, Ident::from_str("debug_tuple"), vec![name]);
|
cx.expr_method_call(span, fmt, cx.ident_of("debug_tuple", span), vec![name]);
|
||||||
stmts.push(cx.stmt_let(span, true, builder, expr));
|
stmts.push(cx.stmt_let(span, true, builder, expr));
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
@ -93,7 +93,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
|
|||||||
ast::VariantData::Struct(..) => {
|
ast::VariantData::Struct(..) => {
|
||||||
// normal struct/struct variant
|
// normal struct/struct variant
|
||||||
let expr =
|
let expr =
|
||||||
cx.expr_method_call(span, fmt, Ident::from_str("debug_struct"), vec![name]);
|
cx.expr_method_call(span, fmt, cx.ident_of("debug_struct", span), vec![name]);
|
||||||
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
|
stmts.push(cx.stmt_let(DUMMY_SP, true, builder, expr));
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
@ -113,7 +113,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let expr = cx.expr_method_call(span, builder_expr, Ident::from_str("finish"), vec![]);
|
let expr = cx.expr_method_call(span, builder_expr, cx.ident_of("finish", span), vec![]);
|
||||||
|
|
||||||
stmts.push(cx.stmt_expr(expr));
|
stmts.push(cx.stmt_expr(expr));
|
||||||
let block = cx.block(span, stmts);
|
let block = cx.block(span, stmts);
|
||||||
|
@ -66,10 +66,14 @@ fn decodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
krate: &str)
|
krate: &str)
|
||||||
-> P<Expr> {
|
-> P<Expr> {
|
||||||
let decoder = substr.nonself_args[0].clone();
|
let decoder = substr.nonself_args[0].clone();
|
||||||
let recurse = vec![cx.ident_of(krate), cx.ident_of("Decodable"), cx.ident_of("decode")];
|
let recurse = vec![
|
||||||
|
cx.ident_of(krate, trait_span),
|
||||||
|
cx.ident_of("Decodable", trait_span),
|
||||||
|
cx.ident_of("decode", trait_span),
|
||||||
|
];
|
||||||
let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
|
let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
|
||||||
// throw an underscore in front to suppress unused variable warnings
|
// throw an underscore in front to suppress unused variable warnings
|
||||||
let blkarg = cx.ident_of("_d");
|
let blkarg = cx.ident_of("_d", trait_span);
|
||||||
let blkdecoder = cx.expr_ident(trait_span, blkarg);
|
let blkdecoder = cx.expr_ident(trait_span, blkarg);
|
||||||
|
|
||||||
return match *substr.fields {
|
return match *substr.fields {
|
||||||
@ -78,7 +82,7 @@ fn decodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
Unnamed(ref fields, _) => fields.len(),
|
Unnamed(ref fields, _) => fields.len(),
|
||||||
Named(ref fields) => fields.len(),
|
Named(ref fields) => fields.len(),
|
||||||
};
|
};
|
||||||
let read_struct_field = cx.ident_of("read_struct_field");
|
let read_struct_field = cx.ident_of("read_struct_field", trait_span);
|
||||||
|
|
||||||
let path = cx.path_ident(trait_span, substr.type_ident);
|
let path = cx.path_ident(trait_span, substr.type_ident);
|
||||||
let result =
|
let result =
|
||||||
@ -94,17 +98,17 @@ fn decodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
let result = cx.expr_ok(trait_span, result);
|
let result = cx.expr_ok(trait_span, result);
|
||||||
cx.expr_method_call(trait_span,
|
cx.expr_method_call(trait_span,
|
||||||
decoder,
|
decoder,
|
||||||
cx.ident_of("read_struct"),
|
cx.ident_of("read_struct", trait_span),
|
||||||
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
||||||
cx.expr_usize(trait_span, nfields),
|
cx.expr_usize(trait_span, nfields),
|
||||||
cx.lambda1(trait_span, result, blkarg)])
|
cx.lambda1(trait_span, result, blkarg)])
|
||||||
}
|
}
|
||||||
StaticEnum(_, ref fields) => {
|
StaticEnum(_, ref fields) => {
|
||||||
let variant = cx.ident_of("i");
|
let variant = cx.ident_of("i", trait_span);
|
||||||
|
|
||||||
let mut arms = Vec::with_capacity(fields.len() + 1);
|
let mut arms = Vec::with_capacity(fields.len() + 1);
|
||||||
let mut variants = Vec::with_capacity(fields.len());
|
let mut variants = Vec::with_capacity(fields.len());
|
||||||
let rvariant_arg = cx.ident_of("read_enum_variant_arg");
|
let rvariant_arg = cx.ident_of("read_enum_variant_arg", trait_span);
|
||||||
|
|
||||||
for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() {
|
for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() {
|
||||||
variants.push(cx.expr_str(v_span, ident.name));
|
variants.push(cx.expr_str(v_span, ident.name));
|
||||||
@ -132,11 +136,11 @@ fn decodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
let variant_vec = cx.expr_addr_of(trait_span, variant_vec);
|
let variant_vec = cx.expr_addr_of(trait_span, variant_vec);
|
||||||
let result = cx.expr_method_call(trait_span,
|
let result = cx.expr_method_call(trait_span,
|
||||||
blkdecoder,
|
blkdecoder,
|
||||||
cx.ident_of("read_enum_variant"),
|
cx.ident_of("read_enum_variant", trait_span),
|
||||||
vec![variant_vec, lambda]);
|
vec![variant_vec, lambda]);
|
||||||
cx.expr_method_call(trait_span,
|
cx.expr_method_call(trait_span,
|
||||||
decoder,
|
decoder,
|
||||||
cx.ident_of("read_enum"),
|
cx.ident_of("read_enum", trait_span),
|
||||||
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
||||||
cx.lambda1(trait_span, result, blkarg)])
|
cx.lambda1(trait_span, result, blkarg)])
|
||||||
}
|
}
|
||||||
|
@ -153,16 +153,16 @@ fn encodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
-> P<Expr> {
|
-> P<Expr> {
|
||||||
let encoder = substr.nonself_args[0].clone();
|
let encoder = substr.nonself_args[0].clone();
|
||||||
// throw an underscore in front to suppress unused variable warnings
|
// throw an underscore in front to suppress unused variable warnings
|
||||||
let blkarg = cx.ident_of("_e");
|
let blkarg = cx.ident_of("_e", trait_span);
|
||||||
let blkencoder = cx.expr_ident(trait_span, blkarg);
|
let blkencoder = cx.expr_ident(trait_span, blkarg);
|
||||||
let fn_path = cx.expr_path(cx.path_global(trait_span,
|
let fn_path = cx.expr_path(cx.path_global(trait_span,
|
||||||
vec![cx.ident_of(krate),
|
vec![cx.ident_of(krate, trait_span),
|
||||||
cx.ident_of("Encodable"),
|
cx.ident_of("Encodable", trait_span),
|
||||||
cx.ident_of("encode")]));
|
cx.ident_of("encode", trait_span)]));
|
||||||
|
|
||||||
return match *substr.fields {
|
return match *substr.fields {
|
||||||
Struct(_, ref fields) => {
|
Struct(_, ref fields) => {
|
||||||
let emit_struct_field = cx.ident_of("emit_struct_field");
|
let emit_struct_field = cx.ident_of("emit_struct_field", trait_span);
|
||||||
let mut stmts = Vec::new();
|
let mut stmts = Vec::new();
|
||||||
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
|
for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() {
|
||||||
let name = match name {
|
let name = match name {
|
||||||
@ -201,7 +201,7 @@ fn encodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
|
|
||||||
cx.expr_method_call(trait_span,
|
cx.expr_method_call(trait_span,
|
||||||
encoder,
|
encoder,
|
||||||
cx.ident_of("emit_struct"),
|
cx.ident_of("emit_struct", trait_span),
|
||||||
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
vec![cx.expr_str(trait_span, substr.type_ident.name),
|
||||||
cx.expr_usize(trait_span, fields.len()),
|
cx.expr_usize(trait_span, fields.len()),
|
||||||
blk])
|
blk])
|
||||||
@ -214,7 +214,7 @@ fn encodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
// actually exist.
|
// actually exist.
|
||||||
let me = cx.stmt_let(trait_span, false, blkarg, encoder);
|
let me = cx.stmt_let(trait_span, false, blkarg, encoder);
|
||||||
let encoder = cx.expr_ident(trait_span, blkarg);
|
let encoder = cx.expr_ident(trait_span, blkarg);
|
||||||
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg");
|
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg", trait_span);
|
||||||
let mut stmts = Vec::new();
|
let mut stmts = Vec::new();
|
||||||
if !fields.is_empty() {
|
if !fields.is_empty() {
|
||||||
let last = fields.len() - 1;
|
let last = fields.len() - 1;
|
||||||
@ -244,7 +244,7 @@ fn encodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
let name = cx.expr_str(trait_span, variant.ident.name);
|
let name = cx.expr_str(trait_span, variant.ident.name);
|
||||||
let call = cx.expr_method_call(trait_span,
|
let call = cx.expr_method_call(trait_span,
|
||||||
blkencoder,
|
blkencoder,
|
||||||
cx.ident_of("emit_enum_variant"),
|
cx.ident_of("emit_enum_variant", trait_span),
|
||||||
vec![name,
|
vec![name,
|
||||||
cx.expr_usize(trait_span, idx),
|
cx.expr_usize(trait_span, idx),
|
||||||
cx.expr_usize(trait_span, fields.len()),
|
cx.expr_usize(trait_span, fields.len()),
|
||||||
@ -252,7 +252,7 @@ fn encodable_substructure(cx: &mut ExtCtxt<'_>,
|
|||||||
let blk = cx.lambda1(trait_span, call, blkarg);
|
let blk = cx.lambda1(trait_span, call, blkarg);
|
||||||
let ret = cx.expr_method_call(trait_span,
|
let ret = cx.expr_method_call(trait_span,
|
||||||
encoder,
|
encoder,
|
||||||
cx.ident_of("emit_enum"),
|
cx.ident_of("emit_enum", trait_span),
|
||||||
vec![cx.expr_str(trait_span ,substr.type_ident.name),
|
vec![cx.expr_str(trait_span ,substr.type_ident.name),
|
||||||
blk]);
|
blk]);
|
||||||
cx.expr_block(cx.block(trait_span, vec![me, cx.stmt_expr(ret)]))
|
cx.expr_block(cx.block(trait_span, vec![me, cx.stmt_expr(ret)]))
|
||||||
|
@ -237,7 +237,7 @@ pub struct MethodDef<'a> {
|
|||||||
/// Whether there is a self argument (outer Option) i.e., whether
|
/// Whether there is a self argument (outer Option) i.e., whether
|
||||||
/// this is a static function, and whether it is a pointer (inner
|
/// this is a static function, and whether it is a pointer (inner
|
||||||
/// Option)
|
/// Option)
|
||||||
pub explicit_self: Option<Option<PtrTy<'a>>>,
|
pub explicit_self: Option<Option<PtrTy>>,
|
||||||
|
|
||||||
/// Arguments other than the self argument
|
/// Arguments other than the self argument
|
||||||
pub args: Vec<(Ty<'a>, &'a str)>,
|
pub args: Vec<(Ty<'a>, &'a str)>,
|
||||||
@ -843,7 +843,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
-> P<Expr> {
|
-> P<Expr> {
|
||||||
let substructure = Substructure {
|
let substructure = Substructure {
|
||||||
type_ident,
|
type_ident,
|
||||||
method_ident: cx.ident_of(self.name),
|
method_ident: cx.ident_of(self.name, trait_.span),
|
||||||
self_args,
|
self_args,
|
||||||
nonself_args,
|
nonself_args,
|
||||||
fields,
|
fields,
|
||||||
@ -890,7 +890,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
|
|
||||||
for (ty, name) in self.args.iter() {
|
for (ty, name) in self.args.iter() {
|
||||||
let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics);
|
let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics);
|
||||||
let ident = ast::Ident::from_str_and_span(name, trait_.span);
|
let ident = cx.ident_of(name, trait_.span);
|
||||||
arg_tys.push((ident, ast_ty));
|
arg_tys.push((ident, ast_ty));
|
||||||
|
|
||||||
let arg_expr = cx.expr_ident(trait_.span, ident);
|
let arg_expr = cx.expr_ident(trait_.span, ident);
|
||||||
@ -938,7 +938,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
|
|
||||||
let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
|
let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
|
||||||
|
|
||||||
let method_ident = cx.ident_of(self.name);
|
let method_ident = cx.ident_of(self.name, trait_.span);
|
||||||
let fn_decl = cx.fn_decl(args, ast::FunctionRetTy::Ty(ret_type));
|
let fn_decl = cx.fn_decl(args, ast::FunctionRetTy::Ty(ret_type));
|
||||||
let body_block = cx.block_expr(body);
|
let body_block = cx.block_expr(body);
|
||||||
|
|
||||||
@ -1201,7 +1201,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
).collect::<Vec<String>>();
|
).collect::<Vec<String>>();
|
||||||
|
|
||||||
let self_arg_idents = self_arg_names.iter()
|
let self_arg_idents = self_arg_names.iter()
|
||||||
.map(|name| cx.ident_of(&name[..]))
|
.map(|name| cx.ident_of(name, sp))
|
||||||
.collect::<Vec<ast::Ident>>();
|
.collect::<Vec<ast::Ident>>();
|
||||||
|
|
||||||
// The `vi_idents` will be bound, solely in the catch-all, to
|
// The `vi_idents` will be bound, solely in the catch-all, to
|
||||||
@ -1210,7 +1210,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
let vi_idents = self_arg_names.iter()
|
let vi_idents = self_arg_names.iter()
|
||||||
.map(|name| {
|
.map(|name| {
|
||||||
let vi_suffix = format!("{}_vi", &name[..]);
|
let vi_suffix = format!("{}_vi", &name[..]);
|
||||||
ast::Ident::from_str_and_span(&vi_suffix[..], trait_.span)
|
cx.ident_of(&vi_suffix[..], trait_.span)
|
||||||
})
|
})
|
||||||
.collect::<Vec<ast::Ident>>();
|
.collect::<Vec<ast::Ident>>();
|
||||||
|
|
||||||
@ -1389,7 +1389,7 @@ impl<'a> MethodDef<'a> {
|
|||||||
|
|
||||||
let target_ty = cx.ty_ident(
|
let target_ty = cx.ty_ident(
|
||||||
sp,
|
sp,
|
||||||
ast::Ident::from_str_and_span(target_type_name, sp),
|
cx.ident_of(target_type_name, sp),
|
||||||
);
|
);
|
||||||
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
|
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
|
||||||
let let_stmt = cx.stmt_let(sp, false, ident, variant_disr);
|
let let_stmt = cx.stmt_let(sp, false, ident, variant_disr);
|
||||||
@ -1591,7 +1591,7 @@ impl<'a> TraitDef<'a> {
|
|||||||
let mut ident_exprs = Vec::new();
|
let mut ident_exprs = Vec::new();
|
||||||
for (i, struct_field) in struct_def.fields().iter().enumerate() {
|
for (i, struct_field) in struct_def.fields().iter().enumerate() {
|
||||||
let sp = struct_field.span.with_ctxt(self.span.ctxt());
|
let sp = struct_field.span.with_ctxt(self.span.ctxt());
|
||||||
let ident = ast::Ident::from_str_and_span(&format!("{}_{}", prefix, i), self.span);
|
let ident = cx.ident_of(&format!("{}_{}", prefix, i), self.span);
|
||||||
paths.push(ident.with_span_pos(sp));
|
paths.push(ident.with_span_pos(sp));
|
||||||
let val = cx.expr_path(cx.path_ident(sp, ident));
|
let val = cx.expr_path(cx.path_ident(sp, ident));
|
||||||
let val = if use_temporaries {
|
let val = if use_temporaries {
|
||||||
|
@ -13,9 +13,9 @@ use syntax_pos::symbol::kw;
|
|||||||
|
|
||||||
/// The types of pointers
|
/// The types of pointers
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum PtrTy<'a> {
|
pub enum PtrTy {
|
||||||
/// &'lifetime mut
|
/// &'lifetime mut
|
||||||
Borrowed(Option<&'a str>, ast::Mutability),
|
Borrowed(Option<Ident>, ast::Mutability),
|
||||||
/// *mut
|
/// *mut
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
Raw(ast::Mutability),
|
Raw(ast::Mutability),
|
||||||
@ -26,7 +26,7 @@ pub enum PtrTy<'a> {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Path<'a> {
|
pub struct Path<'a> {
|
||||||
path: Vec<&'a str>,
|
path: Vec<&'a str>,
|
||||||
lifetime: Option<&'a str>,
|
lifetime: Option<Ident>,
|
||||||
params: Vec<Box<Ty<'a>>>,
|
params: Vec<Box<Ty<'a>>>,
|
||||||
kind: PathKind,
|
kind: PathKind,
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ impl<'a> Path<'a> {
|
|||||||
Path::new_(vec![path], None, Vec::new(), PathKind::Local)
|
Path::new_(vec![path], None, Vec::new(), PathKind::Local)
|
||||||
}
|
}
|
||||||
pub fn new_<'r>(path: Vec<&'r str>,
|
pub fn new_<'r>(path: Vec<&'r str>,
|
||||||
lifetime: Option<&'r str>,
|
lifetime: Option<Ident>,
|
||||||
params: Vec<Box<Ty<'r>>>,
|
params: Vec<Box<Ty<'r>>>,
|
||||||
kind: PathKind)
|
kind: PathKind)
|
||||||
-> Path<'r> {
|
-> Path<'r> {
|
||||||
@ -72,7 +72,7 @@ impl<'a> Path<'a> {
|
|||||||
self_ty: Ident,
|
self_ty: Ident,
|
||||||
self_generics: &Generics)
|
self_generics: &Generics)
|
||||||
-> ast::Path {
|
-> ast::Path {
|
||||||
let mut idents = self.path.iter().map(|s| Ident::from_str_and_span(*s, span)).collect();
|
let mut idents = self.path.iter().map(|s| cx.ident_of(*s, span)).collect();
|
||||||
let lt = mk_lifetimes(cx, span, &self.lifetime);
|
let lt = mk_lifetimes(cx, span, &self.lifetime);
|
||||||
let tys: Vec<P<ast::Ty>> =
|
let tys: Vec<P<ast::Ty>> =
|
||||||
self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
|
self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect();
|
||||||
@ -99,7 +99,7 @@ impl<'a> Path<'a> {
|
|||||||
pub enum Ty<'a> {
|
pub enum Ty<'a> {
|
||||||
Self_,
|
Self_,
|
||||||
/// &/Box/ Ty
|
/// &/Box/ Ty
|
||||||
Ptr(Box<Ty<'a>>, PtrTy<'a>),
|
Ptr(Box<Ty<'a>>, PtrTy),
|
||||||
/// mod::mod::Type<[lifetime], [Params...]>, including a plain type
|
/// mod::mod::Type<[lifetime], [Params...]>, including a plain type
|
||||||
/// parameter, and things like `i32`
|
/// parameter, and things like `i32`
|
||||||
Literal(Path<'a>),
|
Literal(Path<'a>),
|
||||||
@ -107,14 +107,14 @@ pub enum Ty<'a> {
|
|||||||
Tuple(Vec<Ty<'a>>),
|
Tuple(Vec<Ty<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
|
pub fn borrowed_ptrty() -> PtrTy {
|
||||||
Borrowed(None, ast::Mutability::Immutable)
|
Borrowed(None, ast::Mutability::Immutable)
|
||||||
}
|
}
|
||||||
pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
|
pub fn borrowed(ty: Box<Ty<'_>>) -> Ty<'_> {
|
||||||
Ptr(ty, borrowed_ptrty())
|
Ptr(ty, borrowed_ptrty())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
|
pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
|
||||||
Some(Some(borrowed_ptrty()))
|
Some(Some(borrowed_ptrty()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,13 +126,11 @@ pub fn nil_ty<'r>() -> Ty<'r> {
|
|||||||
Tuple(Vec::new())
|
Tuple(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Option<ast::Lifetime> {
|
fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Option<ast::Lifetime> {
|
||||||
lt.map(|s|
|
lt.map(|ident| cx.lifetime(span, ident))
|
||||||
cx.lifetime(span, Ident::from_str(s))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<&str>) -> Vec<ast::Lifetime> {
|
fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Vec<ast::Lifetime> {
|
||||||
mk_lifetime(cx, span, lt).into_iter().collect()
|
mk_lifetime(cx, span, lt).into_iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +207,7 @@ fn mk_ty_param(cx: &ExtCtxt<'_>,
|
|||||||
cx.trait_bound(path)
|
cx.trait_bound(path)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
cx.typaram(span, ast::Ident::from_str_and_span(name, span), attrs.to_owned(), bounds, None)
|
cx.typaram(span, cx.ident_of(name, span), attrs.to_owned(), bounds, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
|
fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
|
||||||
@ -265,7 +263,7 @@ impl<'a> LifetimeBounds<'a> {
|
|||||||
|
|
||||||
pub fn get_explicit_self(cx: &ExtCtxt<'_>,
|
pub fn get_explicit_self(cx: &ExtCtxt<'_>,
|
||||||
span: Span,
|
span: Span,
|
||||||
self_ptr: &Option<PtrTy<'_>>)
|
self_ptr: &Option<PtrTy>)
|
||||||
-> (P<Expr>, ast::ExplicitSelf) {
|
-> (P<Expr>, ast::ExplicitSelf) {
|
||||||
// this constructs a fresh `self` path
|
// this constructs a fresh `self` path
|
||||||
let self_path = cx.expr_self(span);
|
let self_path = cx.expr_self(span);
|
||||||
@ -276,7 +274,7 @@ pub fn get_explicit_self(cx: &ExtCtxt<'_>,
|
|||||||
respan(span,
|
respan(span,
|
||||||
match *ptr {
|
match *ptr {
|
||||||
Borrowed(ref lt, mutbl) => {
|
Borrowed(ref lt, mutbl) => {
|
||||||
let lt = lt.map(|s| cx.lifetime(span, Ident::from_str(s)));
|
let lt = lt.map(|s| cx.lifetime(span, s));
|
||||||
SelfKind::Region(lt, mutbl)
|
SelfKind::Region(lt, mutbl)
|
||||||
}
|
}
|
||||||
Raw(_) => {
|
Raw(_) => {
|
||||||
|
@ -20,16 +20,16 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
|||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
};
|
};
|
||||||
|
|
||||||
let sp = cx.with_legacy_ctxt(sp);
|
let sp = cx.with_def_site_ctxt(sp);
|
||||||
let e = match env::var(&*var.as_str()) {
|
let e = match env::var(&*var.as_str()) {
|
||||||
Err(..) => {
|
Err(..) => {
|
||||||
let lt = cx.lifetime(sp, Ident::with_dummy_span(kw::StaticLifetime));
|
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
|
||||||
cx.expr_path(cx.path_all(sp,
|
cx.expr_path(cx.path_all(sp,
|
||||||
true,
|
true,
|
||||||
cx.std_path(&[sym::option, sym::Option, sym::None]),
|
cx.std_path(&[sym::option, sym::Option, sym::None]),
|
||||||
vec![GenericArg::Type(cx.ty_rptr(sp,
|
vec![GenericArg::Type(cx.ty_rptr(sp,
|
||||||
cx.ty_ident(sp,
|
cx.ty_ident(sp,
|
||||||
Ident::with_dummy_span(sym::str)),
|
Ident::new(sym::str, sp)),
|
||||||
Some(lt),
|
Some(lt),
|
||||||
ast::Mutability::Immutable))],
|
ast::Mutability::Immutable))],
|
||||||
vec![]))
|
vec![]))
|
||||||
|
@ -486,7 +486,7 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
let sp = self.macsp;
|
let sp = self.macsp;
|
||||||
let count = |c, arg| {
|
let count = |c, arg| {
|
||||||
let mut path = Context::rtpath(self.ecx, "Count");
|
let mut path = Context::rtpath(self.ecx, "Count");
|
||||||
path.push(self.ecx.ident_of(c));
|
path.push(self.ecx.ident_of(c, sp));
|
||||||
match arg {
|
match arg {
|
||||||
Some(arg) => self.ecx.expr_call_global(sp, path, vec![arg]),
|
Some(arg) => self.ecx.expr_call_global(sp, path, vec![arg]),
|
||||||
None => self.ecx.expr_path(self.ecx.path_global(sp, path)),
|
None => self.ecx.expr_path(self.ecx.path_global(sp, path)),
|
||||||
@ -534,7 +534,7 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
let pos = {
|
let pos = {
|
||||||
let pos = |c, arg| {
|
let pos = |c, arg| {
|
||||||
let mut path = Context::rtpath(self.ecx, "Position");
|
let mut path = Context::rtpath(self.ecx, "Position");
|
||||||
path.push(self.ecx.ident_of(c));
|
path.push(self.ecx.ident_of(c, sp));
|
||||||
match arg {
|
match arg {
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
let arg = self.ecx.expr_usize(sp, i);
|
let arg = self.ecx.expr_usize(sp, i);
|
||||||
@ -603,7 +603,7 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill));
|
let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill));
|
||||||
let align = |name| {
|
let align = |name| {
|
||||||
let mut p = Context::rtpath(self.ecx, "Alignment");
|
let mut p = Context::rtpath(self.ecx, "Alignment");
|
||||||
p.push(self.ecx.ident_of(name));
|
p.push(self.ecx.ident_of(name, sp));
|
||||||
self.ecx.path_global(sp, p)
|
self.ecx.path_global(sp, p)
|
||||||
};
|
};
|
||||||
let align = match arg.format.align {
|
let align = match arg.format.align {
|
||||||
@ -621,11 +621,11 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
sp,
|
sp,
|
||||||
path,
|
path,
|
||||||
vec![
|
vec![
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill),
|
self.ecx.field_imm(sp, self.ecx.ident_of("fill", sp), fill),
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("align"), align),
|
self.ecx.field_imm(sp, self.ecx.ident_of("align", sp), align),
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("flags"), flags),
|
self.ecx.field_imm(sp, self.ecx.ident_of("flags", sp), flags),
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("precision"), prec),
|
self.ecx.field_imm(sp, self.ecx.ident_of("precision", sp), prec),
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("width"), width),
|
self.ecx.field_imm(sp, self.ecx.ident_of("width", sp), width),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -634,8 +634,8 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
sp,
|
sp,
|
||||||
path,
|
path,
|
||||||
vec![
|
vec![
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
|
self.ecx.field_imm(sp, self.ecx.ident_of("position", sp), pos),
|
||||||
self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt),
|
self.ecx.field_imm(sp, self.ecx.ident_of("format", sp), fmt),
|
||||||
],
|
],
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -653,7 +653,7 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||||||
let mut heads = Vec::with_capacity(self.args.len());
|
let mut heads = Vec::with_capacity(self.args.len());
|
||||||
|
|
||||||
let names_pos: Vec<_> = (0..self.args.len())
|
let names_pos: Vec<_> = (0..self.args.len())
|
||||||
.map(|i| ast::Ident::from_str_and_span(&format!("arg{}", i), self.macsp))
|
.map(|i| self.ecx.ident_of(&format!("arg{}", i), self.macsp))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// First, build up the static array which will become our precompiled
|
// First, build up the static array which will become our precompiled
|
||||||
|
@ -28,7 +28,7 @@ pub fn expand(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Generate a bunch of new items using the AllocFnFactory
|
// Generate a bunch of new items using the AllocFnFactory
|
||||||
let span = ecx.with_legacy_ctxt(item.span);
|
let span = ecx.with_def_site_ctxt(item.span);
|
||||||
let f = AllocFnFactory {
|
let f = AllocFnFactory {
|
||||||
span,
|
span,
|
||||||
kind: AllocatorKind::Global,
|
kind: AllocatorKind::Global,
|
||||||
@ -43,7 +43,7 @@ pub fn expand(
|
|||||||
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
|
let const_ty = ecx.ty(span, TyKind::Tup(Vec::new()));
|
||||||
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
||||||
let const_item =
|
let const_item =
|
||||||
ecx.item_const(span, Ident::with_dummy_span(kw::Underscore), const_ty, const_body);
|
ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
|
||||||
|
|
||||||
// Return the original item and the new methods.
|
// Return the original item and the new methods.
|
||||||
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
|
vec![Annotatable::Item(item), Annotatable::Item(const_item)]
|
||||||
@ -61,7 +61,7 @@ impl AllocFnFactory<'_, '_> {
|
|||||||
let mut abi_args = Vec::new();
|
let mut abi_args = Vec::new();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let ref mut mk = || {
|
let ref mut mk = || {
|
||||||
let name = Ident::from_str(&format!("arg{}", i));
|
let name = self.cx.ident_of(&format!("arg{}", i), self.span);
|
||||||
i += 1;
|
i += 1;
|
||||||
name
|
name
|
||||||
};
|
};
|
||||||
@ -83,7 +83,7 @@ impl AllocFnFactory<'_, '_> {
|
|||||||
);
|
);
|
||||||
let item = self.cx.item(
|
let item = self.cx.item(
|
||||||
self.span,
|
self.span,
|
||||||
Ident::from_str(&self.kind.fn_name(method.name)),
|
self.cx.ident_of(&self.kind.fn_name(method.name), self.span),
|
||||||
self.attrs(),
|
self.attrs(),
|
||||||
kind,
|
kind,
|
||||||
);
|
);
|
||||||
@ -119,7 +119,7 @@ impl AllocFnFactory<'_, '_> {
|
|||||||
) -> P<Expr> {
|
) -> P<Expr> {
|
||||||
match *ty {
|
match *ty {
|
||||||
AllocatorTy::Layout => {
|
AllocatorTy::Layout => {
|
||||||
let usize = self.cx.path_ident(self.span, Ident::with_dummy_span(sym::usize));
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
||||||
let ty_usize = self.cx.ty_path(usize);
|
let ty_usize = self.cx.ty_path(usize);
|
||||||
let size = ident();
|
let size = ident();
|
||||||
let align = ident();
|
let align = ident();
|
||||||
@ -177,12 +177,12 @@ impl AllocFnFactory<'_, '_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usize(&self) -> P<Ty> {
|
fn usize(&self) -> P<Ty> {
|
||||||
let usize = self.cx.path_ident(self.span, Ident::with_dummy_span(sym::usize));
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
||||||
self.cx.ty_path(usize)
|
self.cx.ty_path(usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ptr_u8(&self) -> P<Ty> {
|
fn ptr_u8(&self) -> P<Ty> {
|
||||||
let u8 = self.cx.path_ident(self.span, Ident::with_dummy_span(sym::u8));
|
let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
|
||||||
let ty_u8 = self.cx.ty_path(u8);
|
let ty_u8 = self.cx.ty_path(u8);
|
||||||
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
|
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
|||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
node: ast::ItemKind::GlobalAsm(P(global_asm)),
|
node: ast::ItemKind::GlobalAsm(P(global_asm)),
|
||||||
vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||||
span: cx.with_legacy_ctxt(sp),
|
span: cx.with_def_site_ctxt(sp),
|
||||||
tokens: None,
|
tokens: None,
|
||||||
})])
|
})])
|
||||||
}
|
}
|
||||||
|
@ -340,12 +340,12 @@ fn mk_decls(
|
|||||||
Vec::new(),
|
Vec::new(),
|
||||||
ast::ItemKind::ExternCrate(None));
|
ast::ItemKind::ExternCrate(None));
|
||||||
|
|
||||||
let bridge = Ident::from_str_and_span("bridge", span);
|
let bridge = cx.ident_of("bridge", span);
|
||||||
let client = Ident::from_str_and_span("client", span);
|
let client = cx.ident_of("client", span);
|
||||||
let proc_macro_ty = Ident::from_str_and_span("ProcMacro", span);
|
let proc_macro_ty = cx.ident_of("ProcMacro", span);
|
||||||
let custom_derive = Ident::from_str_and_span("custom_derive", span);
|
let custom_derive = cx.ident_of("custom_derive", span);
|
||||||
let attr = Ident::from_str_and_span("attr", span);
|
let attr = cx.ident_of("attr", span);
|
||||||
let bang = Ident::from_str_and_span("bang", span);
|
let bang = cx.ident_of("bang", span);
|
||||||
|
|
||||||
let decls = {
|
let decls = {
|
||||||
let local_path = |sp: Span, name| {
|
let local_path = |sp: Span, name| {
|
||||||
@ -378,7 +378,7 @@ fn mk_decls(
|
|||||||
|
|
||||||
let decls_static = cx.item_static(
|
let decls_static = cx.item_static(
|
||||||
span,
|
span,
|
||||||
Ident::from_str_and_span("_DECLS", span),
|
cx.ident_of("_DECLS", span),
|
||||||
cx.ty_rptr(span,
|
cx.ty_rptr(span,
|
||||||
cx.ty(span, ast::TyKind::Slice(
|
cx.ty(span, ast::TyKind::Slice(
|
||||||
cx.ty_path(cx.path(span,
|
cx.ty_path(cx.path(span,
|
||||||
|
@ -98,20 +98,20 @@ pub fn expand_test_or_bench(
|
|||||||
|
|
||||||
// creates test::$name
|
// creates test::$name
|
||||||
let test_path = |name| {
|
let test_path = |name| {
|
||||||
cx.path(sp, vec![test_id, cx.ident_of(name)])
|
cx.path(sp, vec![test_id, cx.ident_of(name, sp)])
|
||||||
};
|
};
|
||||||
|
|
||||||
// creates test::ShouldPanic::$name
|
// creates test::ShouldPanic::$name
|
||||||
let should_panic_path = |name| {
|
let should_panic_path = |name| {
|
||||||
cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic"), cx.ident_of(name)])
|
cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)])
|
||||||
};
|
};
|
||||||
|
|
||||||
// creates $name: $expr
|
// creates $name: $expr
|
||||||
let field = |name, expr| cx.field_imm(sp, cx.ident_of(name), expr);
|
let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr);
|
||||||
|
|
||||||
let test_fn = if is_bench {
|
let test_fn = if is_bench {
|
||||||
// A simple ident for a lambda
|
// A simple ident for a lambda
|
||||||
let b = ast::Ident::from_str_and_span("b", attr_sp);
|
let b = cx.ident_of("b", attr_sp);
|
||||||
|
|
||||||
cx.expr_call(sp, cx.expr_path(test_path("StaticBenchFn")), vec![
|
cx.expr_call(sp, cx.expr_path(test_path("StaticBenchFn")), vec![
|
||||||
// |b| self::test::assert_test_result(
|
// |b| self::test::assert_test_result(
|
||||||
|
@ -250,7 +250,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
|
|||||||
|
|
||||||
// test::test_main_static(...)
|
// test::test_main_static(...)
|
||||||
let mut test_runner = cx.test_runner.clone().unwrap_or(
|
let mut test_runner = cx.test_runner.clone().unwrap_or(
|
||||||
ecx.path(sp, vec![test_id, Ident::from_str_and_span("test_main_static", sp)]));
|
ecx.path(sp, vec![test_id, ecx.ident_of("test_main_static", sp)]));
|
||||||
|
|
||||||
test_runner.span = sp;
|
test_runner.span = sp;
|
||||||
|
|
||||||
@ -288,7 +288,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
|
|||||||
// Honor the reexport_test_harness_main attribute
|
// Honor the reexport_test_harness_main attribute
|
||||||
let main_id = match cx.reexport_test_harness_main {
|
let main_id = match cx.reexport_test_harness_main {
|
||||||
Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
|
Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
|
||||||
None => Ident::from_str_and_span("main", sp),
|
None => Ident::new(sym::main, sp),
|
||||||
};
|
};
|
||||||
|
|
||||||
let main = P(ast::Item {
|
let main = P(ast::Item {
|
||||||
|
@ -526,13 +526,6 @@ impl Span {
|
|||||||
self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
|
self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Span with a context reproducing `macro_rules` hygiene (hygienic locals, unhygienic items).
|
|
||||||
/// FIXME: This should be eventually replaced either with `with_def_site_ctxt` (preferably),
|
|
||||||
/// or with `with_call_site_ctxt` (where necessary).
|
|
||||||
pub fn with_legacy_ctxt(&self, expn_id: ExpnId) -> Span {
|
|
||||||
self.with_ctxt_from_mark(expn_id, Transparency::SemiTransparent)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Produces a span with the same location as `self` and context produced by a macro with the
|
/// Produces a span with the same location as `self` and context produced by a macro with the
|
||||||
/// given ID and transparency, assuming that macro was defined directly and not produced by
|
/// given ID and transparency, assuming that macro was defined directly and not produced by
|
||||||
/// some other macro (which is the case for built-in and procedural macros).
|
/// some other macro (which is the case for built-in and procedural macros).
|
||||||
|
@ -765,7 +765,7 @@ impl Ident {
|
|||||||
Ident::with_dummy_span(string.as_symbol())
|
Ident::with_dummy_span(string.as_symbol())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps a string to an identifier with an empty span.
|
/// Maps a string to an identifier with a dummy span.
|
||||||
pub fn from_str(string: &str) -> Ident {
|
pub fn from_str(string: &str) -> Ident {
|
||||||
Ident::with_dummy_span(Symbol::intern(string))
|
Ident::with_dummy_span(Symbol::intern(string))
|
||||||
}
|
}
|
||||||
|
31
src/test/ui/allocator/hygiene.rs
Normal file
31
src/test/ui/allocator/hygiene.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// run-pass
|
||||||
|
// no-prefer-dynamic
|
||||||
|
// aux-build:custom.rs
|
||||||
|
// aux-build:helper.rs
|
||||||
|
|
||||||
|
#![allow(nonstandard_style)]
|
||||||
|
|
||||||
|
extern crate custom;
|
||||||
|
extern crate helper;
|
||||||
|
|
||||||
|
use custom::A;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct u8;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct usize;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
static arg0: () = ();
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
pub static GLOBAL: A = A(AtomicUsize::new(0));
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let n = GLOBAL.0.load(Ordering::SeqCst);
|
||||||
|
let s = Box::new(0);
|
||||||
|
helper::work_with(&s);
|
||||||
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
|
||||||
|
drop(s);
|
||||||
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
// run-pass
|
||||||
|
|
||||||
#![feature(concat_idents)]
|
#![feature(concat_idents)]
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
@ -5,10 +7,8 @@ pub fn main() {
|
|||||||
let _: concat_idents!(F, oo) = Foo; // Test that `concat_idents!` can be used in type positions
|
let _: concat_idents!(F, oo) = Foo; // Test that `concat_idents!` can be used in type positions
|
||||||
|
|
||||||
let asdf_fdsa = "<.<".to_string();
|
let asdf_fdsa = "<.<".to_string();
|
||||||
// this now fails (correctly, I claim) because hygiene prevents
|
// concat_idents should have call-site hygiene.
|
||||||
// the assembled identifier from being a reference to the binding.
|
|
||||||
assert!(concat_idents!(asd, f_f, dsa) == "<.<".to_string());
|
assert!(concat_idents!(asd, f_f, dsa) == "<.<".to_string());
|
||||||
//~^ ERROR cannot find value `asdf_fdsa` in this scope
|
|
||||||
|
|
||||||
assert_eq!(stringify!(use_mention_distinction), "use_mention_distinction");
|
assert_eq!(stringify!(use_mention_distinction), "use_mention_distinction");
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
error[E0425]: cannot find value `asdf_fdsa` in this scope
|
|
||||||
--> $DIR/syntax-extension-minor.rs:10:13
|
|
||||||
|
|
|
||||||
LL | assert!(concat_idents!(asd, f_f, dsa) == "<.<".to_string());
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0425`.
|
|
Loading…
Reference in New Issue
Block a user