RUST_CONFIGURE_ARGS=--build=x86_64-apple-darwin
SRC=.
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
+ SCCACHE_ERROR_LOG=/tmp/sccache.log
+ RUST_LOG=sccache
os: osx
osx_image: xcode8.2
install: &osx_install_sccache >
RUST_CONFIGURE_ARGS=--build=i686-apple-darwin
SRC=.
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
+ SCCACHE_ERROR_LOG=/tmp/sccache.log
+ RUST_LOG=sccache
os: osx
osx_image: xcode8.2
install: *osx_install_sccache
SRC=.
DEPLOY=1
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
+ SCCACHE_ERROR_LOG=/tmp/sccache.log
+ RUST_LOG=sccache
os: osx
osx_image: xcode8.2
install: >
SRC=.
DEPLOY=1
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
+ SCCACHE_ERROR_LOG=/tmp/sccache.log
+ RUST_LOG=sccache
os: osx
osx_image: xcode8.2
install: *osx_install_sccache
SRC=.
DEPLOY_ALT=1
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
+ SCCACHE_ERROR_LOG=/tmp/sccache.log
+ RUST_LOG=sccache
os: osx
osx_image: xcode8.2
install: *osx_install_sccache
df -h;
du . | sort -nr | head -n100
- cat obj/tmp/sccache.log
+ - cat /tmp/sccache.log
# Save tagged docker images we created and load them if they're available
before_cache:
- set PATH=%PATH%;%CD%\handle
- handle.exe -accepteula -help
+ # Attempt to debug sccache failures
+ - set RUST_LOG=sccache
+ - set SCCACHE_ERROR_LOG=%CD%/sccache.log
+
test_script:
- appveyor-retry sh -c 'git submodule deinit -f . && git submodule update --init'
- set SRC=.
- set NO_CCACHE=1
- sh src/ci/run.sh
+on_failure:
+ - cat %CD%/sccache.log
+
cache:
- "build/i686-pc-windows-msvc/llvm -> src/rustllvm/llvm-auto-clean-trigger"
- "build/x86_64-pc-windows-msvc/llvm -> src/rustllvm/llvm-auto-clean-trigger"
cmd.arg(distdir(build));
cmd.arg(today.trim());
cmd.arg(build.rust_package_vers());
- cmd.arg(build.cargo_info.version(build, &build.cargo_release_num()));
+ cmd.arg(build.package_vers(&build.cargo_release_num()));
cmd.arg(addr);
t!(fs::create_dir_all(distdir(build)));
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Display {
/// Formats the value using the given formatter.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fmt;
+ ///
+ /// struct Position {
+ /// longitude: f32,
+ /// latitude: f32,
+ /// }
+ ///
+ /// impl fmt::Display for Position {
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// write!(f, "({}, {})", self.longitude, self.latitude)
+ /// }
+ /// }
+ ///
+ /// assert_eq!("(1.987, 2.983)".to_owned(),
+ /// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
+ /// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn fmt(&self, f: &mut Formatter) -> Result;
}
}
impl<'a> Formatter<'a> {
-
// First up is the collection of functions used to execute a format string
// at runtime. This consumes all of the compile-time statics generated by
// the format! syntax extension.
use super::dep_node::{DepNode, WorkProductId};
use super::query::DepGraphQuery;
use super::raii;
+use super::safe::DepGraphSafe;
use super::thread::{DepGraphThreadData, DepMessage};
#[derive(Clone)]
op()
}
- pub fn with_task<OP,R>(&self, key: DepNode<DefId>, op: OP) -> R
- where OP: FnOnce() -> R
+ /// Starts a new dep-graph task. Dep-graph tasks are specified
+ /// using a free function (`task`) and **not** a closure -- this
+ /// is intentional because we want to exercise tight control over
+ /// what state they have access to. In particular, we want to
+ /// prevent implicit 'leaks' of tracked state into the task (which
+ /// could then be read without generating correct edges in the
+ /// dep-graph -- see the [README] for more details on the
+ /// dep-graph). To this end, the task function gets exactly two
+ /// pieces of state: the context `cx` and an argument `arg`. Both
+ /// of these bits of state must be of some type that implements
+ /// `DepGraphSafe` and hence does not leak.
+ ///
+ /// The choice of two arguments is not fundamental. One argument
+ /// would work just as well, since multiple values can be
+ /// collected using tuples. However, using two arguments works out
+ /// to be quite convenient, since it is common to need a context
+ /// (`cx`) and some argument (e.g., a `DefId` identifying what
+ /// item to process).
+ ///
+ /// For cases where you need some other number of arguments:
+ ///
+ /// - If you only need one argument, just use `()` for the `arg`
+ /// parameter.
+ /// - If you need 3+ arguments, use a tuple for the
+ /// `arg` parameter.
+ ///
+ /// [README]: README.md
+ pub fn with_task<C, A, R>(&self, key: DepNode<DefId>, cx: C, arg: A, task: fn(C, A) -> R) -> R
+ where C: DepGraphSafe, A: DepGraphSafe
{
let _task = self.in_task(key);
- op()
+ task(cx, arg)
}
pub fn read(&self, v: DepNode<DefId>) {
mod graph;
mod query;
mod raii;
+mod safe;
mod shadow;
mod thread;
mod visit;
pub use self::graph::DepGraph;
pub use self::graph::WorkProduct;
pub use self::query::DepGraphQuery;
+pub use self::safe::AssertDepGraphSafe;
+pub use self::safe::DepGraphSafe;
pub use self::visit::visit_all_bodies_in_krate;
pub use self::visit::visit_all_item_likes_in_krate;
pub use self::raii::DepTask;
--- /dev/null
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use hir::BodyId;
+use hir::def_id::DefId;
+use syntax::ast::NodeId;
+use ty::TyCtxt;
+
+/// The `DepGraphSafe` trait is used to specify what kinds of values
+/// are safe to "leak" into a task. The idea is that this should be
+/// only be implemented for things like the tcx as well as various id
+/// types, which will create reads in the dep-graph whenever the trait
+/// loads anything that might depend on the input program.
+pub trait DepGraphSafe {
+}
+
+/// A `BodyId` on its own doesn't give access to any particular state.
+/// You must fetch the state from the various maps or generate
+/// on-demand queries, all of which create reads.
+impl DepGraphSafe for BodyId {
+}
+
+/// A `NodeId` on its own doesn't give access to any particular state.
+/// You must fetch the state from the various maps or generate
+/// on-demand queries, all of which create reads.
+impl DepGraphSafe for NodeId {
+}
+
+/// A `DefId` on its own doesn't give access to any particular state.
+/// You must fetch the state from the various maps or generate
+/// on-demand queries, all of which create reads.
+impl DepGraphSafe for DefId {
+}
+
+/// The type context itself can be used to access all kinds of tracked
+/// state, but those accesses should always generate read events.
+impl<'a, 'gcx, 'tcx> DepGraphSafe for TyCtxt<'a, 'gcx, 'tcx> {
+}
+
+/// Tuples make it easy to build up state.
+impl<A, B> DepGraphSafe for (A, B)
+ where A: DepGraphSafe, B: DepGraphSafe
+{
+}
+
+/// No data here! :)
+impl DepGraphSafe for () {
+}
+
+/// A convenient override that lets you pass arbitrary state into a
+/// task. Every use should be accompanied by a comment explaining why
+/// it makes sense (or how it could be refactored away in the future).
+pub struct AssertDepGraphSafe<T>(pub T);
+
+impl<T> DepGraphSafe for AssertDepGraphSafe<T> {
+}
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
bodies: BTreeMap<hir::BodyId, hir::Body>,
+ exported_macros: Vec<hir::MacroDef>,
trait_impls: BTreeMap<DefId, Vec<NodeId>>,
trait_default_impl: BTreeMap<DefId, NodeId>,
bodies: BTreeMap::new(),
trait_impls: BTreeMap::new(),
trait_default_impl: BTreeMap::new(),
+ exported_macros: Vec::new(),
loop_scopes: Vec::new(),
is_in_loop_condition: false,
type_def_lifetime_params: DefIdMap(),
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
fn visit_item(&mut self, item: &'lcx Item) {
- let hir_item = self.lctx.lower_item(item);
- self.lctx.items.insert(item.id, hir_item);
- visit::walk_item(self, item);
+ if let Some(hir_item) = self.lctx.lower_item(item) {
+ self.lctx.items.insert(item.id, hir_item);
+ visit::walk_item(self, item);
+ }
}
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
let module = self.lower_mod(&c.module);
let attrs = self.lower_attrs(&c.attrs);
- let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
let body_ids = body_ids(&self.bodies);
hir::Crate {
module: module,
attrs: attrs,
span: c.span,
- exported_macros: exported_macros,
+ exported_macros: hir::HirVec::from(self.exported_macros),
items: self.items,
trait_items: self.trait_items,
impl_items: self.impl_items,
bounds,
items)
}
- ItemKind::Mac(_) => panic!("Shouldn't still be around"),
+ ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
}
}
}
}
- fn lower_macro_def(&mut self, m: &MacroDef) -> hir::MacroDef {
- hir::MacroDef {
- name: m.ident.name,
- attrs: self.lower_attrs(&m.attrs),
- id: m.id,
- span: m.span,
- body: m.body.clone().into(),
- }
- }
-
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
- if let ItemKind::Use(ref view_path) = i.node {
- if let ViewPathList(_, ref imports) = view_path.node {
- return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
- .map(|id| hir::ItemId { id: id }).collect();
+ match i.node {
+ ItemKind::Use(ref view_path) => {
+ if let ViewPathList(_, ref imports) = view_path.node {
+ return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
+ .map(|id| hir::ItemId { id: id }).collect();
+ }
}
+ ItemKind::MacroDef(..) => return SmallVector::new(),
+ _ => {}
}
SmallVector::one(hir::ItemId { id: i.id })
}
- pub fn lower_item(&mut self, i: &Item) -> hir::Item {
+ pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
let mut name = i.ident.name;
let attrs = self.lower_attrs(&i.attrs);
let mut vis = self.lower_visibility(&i.vis);
+ if let ItemKind::MacroDef(ref tts) = i.node {
+ if i.attrs.iter().any(|attr| attr.name() == "macro_export") {
+ self.exported_macros.push(hir::MacroDef {
+ name: name, attrs: attrs, id: i.id, span: i.span, body: tts.clone().into(),
+ });
+ }
+ return None;
+ }
+
let node = self.with_parent_def(i.id, |this| {
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
});
- hir::Item {
+ Some(hir::Item {
id: i.id,
name: name,
attrs: attrs,
node: node,
vis: vis,
span: i.span,
- }
+ })
}
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem {
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
DefPathData::ValueNs(i.ident.name.as_str()),
- ItemKind::Mac(..) if i.id == DUMMY_NODE_ID => return, // Scope placeholder
+ ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
ItemKind::Use(ref view_path) => {
match view_path.node {
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
}
- fn visit_macro_def(&mut self, macro_def: &'a MacroDef) {
- self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.ident.name.as_str()));
- }
-
fn visit_stmt(&mut self, stmt: &'a Stmt) {
match stmt.node {
StmtKind::Mac(..) => self.visit_macro_invoc(stmt.id, false),
The main case which fails today that I would like to support is:
-```text
+```rust
fn foo<T>(x: T, y: T) { ... }
fn bar() {
`X`, and thus inherits its UB/LB of `@mut int`. This leaves no
flexibility for `T` to later adjust to accommodate `@int`.
+Note: `@` and `@mut` are replaced with `Rc<T>` and `Rc<RefCell<T>>` in current Rust.
+
### What to do when not all bounds are present
In the prior discussion we assumed that A.ub was not top and B.lb was
"execute" by testing the value they are applied to and creating any
relevant bindings). So, for example:
- fn foo(x: isize, y: isize) { // -+
- // +------------+ // |
- // | +-----+ // |
- // | +-+ +-+ +-+ // |
- // | | | | | | | // |
- // v v v v v v v // |
- let z = x + y; // |
- ... // |
- } // -+
-
- fn bar() { ... }
+```rust
+fn foo(x: isize, y: isize) { // -+
+// +------------+ // |
+// | +-----+ // |
+// | +-+ +-+ +-+ // |
+// | | | | | | | // |
+// v v v v v v v // |
+ let z = x + y; // |
+ ... // |
+} // -+
+
+fn bar() { ... }
+```
In this example, there is a region for the fn body block as a whole,
and then a subregion for the declaration of the local variable.
particular when combined with `&mut` functions. For example, a call
like this one
- self.foo(self.bar())
+```rust
+self.foo(self.bar())
+```
where both `foo` and `bar` are `&mut self` functions will always yield
an error.
Here is a more involved example (which is safe) so we can see what's
going on:
- struct Foo { f: usize, g: usize }
- ...
- fn add(p: &mut usize, v: usize) {
- *p += v;
- }
- ...
- fn inc(p: &mut usize) -> usize {
- *p += 1; *p
- }
- fn weird() {
- let mut x: Box<Foo> = box Foo { ... };
- 'a: add(&mut (*x).f,
- 'b: inc(&mut (*x).f)) // (..)
- }
+```rust
+struct Foo { f: usize, g: usize }
+// ...
+fn add(p: &mut usize, v: usize) {
+ *p += v;
+}
+// ...
+fn inc(p: &mut usize) -> usize {
+ *p += 1; *p
+}
+fn weird() {
+ let mut x: Box<Foo> = box Foo { /* ... */ };
+ 'a: add(&mut (*x).f,
+ 'b: inc(&mut (*x).f)) // (..)
+}
+```
The important part is the line marked `(..)` which contains a call to
`add()`. The first argument is a mutable borrow of the field `f`. The
involved with `'a` in detail. We'll break apart all the steps involved
in a call expression:
- 'a: {
- 'a_arg1: let a_temp1: ... = add;
- 'a_arg2: let a_temp2: &'a mut usize = &'a mut (*x).f;
- 'a_arg3: let a_temp3: usize = {
- let b_temp1: ... = inc;
- let b_temp2: &'b = &'b mut (*x).f;
- 'b_call: b_temp1(b_temp2)
- };
- 'a_call: a_temp1(a_temp2, a_temp3) // (**)
- }
+```rust
+'a: {
+ 'a_arg1: let a_temp1: ... = add;
+ 'a_arg2: let a_temp2: &'a mut usize = &'a mut (*x).f;
+ 'a_arg3: let a_temp3: usize = {
+ let b_temp1: ... = inc;
+ let b_temp2: &'b = &'b mut (*x).f;
+ 'b_call: b_temp1(b_temp2)
+ };
+ 'a_call: a_temp1(a_temp2, a_temp3) // (**)
+}
+```
Here we see that the lifetime `'a` includes a number of substatements.
In particular, there is this lifetime I've called `'a_call` that
argument, it can still be *invalidated* by that evaluation. Consider
this similar but unsound example:
- struct Foo { f: usize, g: usize }
- ...
- fn add(p: &mut usize, v: usize) {
- *p += v;
- }
- ...
- fn consume(x: Box<Foo>) -> usize {
- x.f + x.g
- }
- fn weird() {
- let mut x: Box<Foo> = box Foo { ... };
- 'a: add(&mut (*x).f, consume(x)) // (..)
- }
+```rust
+struct Foo { f: usize, g: usize }
+// ...
+fn add(p: &mut usize, v: usize) {
+ *p += v;
+}
+// ...
+fn consume(x: Box<Foo>) -> usize {
+ x.f + x.g
+}
+fn weird() {
+ let mut x: Box<Foo> = box Foo { ... };
+ 'a: add(&mut (*x).f, consume(x)) // (..)
+}
+```
In this case, the second argument to `add` actually consumes `x`, thus
invalidating the first argument.
};
if output_template.is_empty() {
- bug!("empty string provided as RUST_REGION_GRAPH");
+ panic!("empty string provided as RUST_REGION_GRAPH");
}
if output_template.contains('%') {
self.tables = old_tables;
}
+ fn visit_body(&mut self, body: &'tcx hir::Body) {
+ run_lints!(self, check_body, late_passes, body);
+ hir_visit::walk_body(self, body);
+ run_lints!(self, check_body_post, late_passes, body);
+ }
+
fn visit_item(&mut self, it: &'tcx hir::Item) {
self.with_lint_attrs(&it.attrs, |cx| {
run_lints!(cx, check_item, late_passes, it);
// FIXME: eliminate the duplication with `Visitor`. But this also
// contains a few lint-specific methods with no equivalent in `Visitor`.
pub trait LateLintPass<'a, 'tcx>: LintPass {
+ fn check_body(&mut self, _: &LateContext, _: &'tcx hir::Body) { }
+ fn check_body_post(&mut self, _: &LateContext, _: &'tcx hir::Body) { }
fn check_name(&mut self, _: &LateContext, _: Span, _: ast::Name) { }
fn check_crate(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx hir::Crate) { }
fn check_crate_post(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx hir::Crate) { }
}
pub enum LoadedMacro {
- MacroRules(ast::MacroDef),
+ MacroDef(ast::Item),
ProcMacro(Rc<SyntaxExtension>),
}
ObjectSafetyViolation,
};
+use errors::DiagnosticBuilder;
use fmt_macros::{Parser, Piece, Position};
+use hir::{intravisit, Local, Pat};
+use hir::intravisit::{Visitor, NestedVisitorMap};
+use hir::map::NodeExpr;
use hir::def_id::DefId;
use infer::{self, InferCtxt};
use infer::type_variable::TypeVariableOrigin;
use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
+use std::fmt;
+use syntax::ast;
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::error::ExpectedFound;
use ty::fast_reject;
use ty::subst::Subst;
use util::nodemap::{FxHashMap, FxHashSet};
-use std::fmt;
-use syntax::ast;
-use hir::{intravisit, Local, Pat};
-use hir::intravisit::{Visitor, NestedVisitorMap};
use syntax_pos::{DUMMY_SP, Span};
-use errors::DiagnosticBuilder;
+
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct TraitErrorKey<'tcx> {
err.span_label(cause.span, &format!("cannot infer type for `{}`", name));
- let expr = self.tcx.hir.expect_expr(cause.body_id);
-
let mut local_visitor = FindLocalByTypeVisitor {
infcx: &self,
target_ty: &ty,
found_pattern: None,
};
- local_visitor.visit_expr(expr);
+ // #40294: cause.body_id can also be a fn declaration.
+ // Currently, if it's anything other than NodeExpr, we just ignore it
+ match self.tcx.hir.find(cause.body_id) {
+ Some(NodeExpr(expr)) => local_visitor.visit_expr(expr),
+ _ => ()
+ }
if let Some(pattern) = local_visitor.found_pattern {
let pattern_span = pattern.span;
let new_trait = tcx.mk_dynamic(
ty::Binder(tcx.mk_existential_predicates(iter)), r_b);
let InferOk { obligations, .. } =
- self.infcx.sub_types(false, &obligation.cause, new_trait, target)
+ self.infcx.eq_types(false, &obligation.cause, new_trait, target)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);
// [T; n] -> [T].
(&ty::TyArray(a, _), &ty::TySlice(b)) => {
let InferOk { obligations, .. } =
- self.infcx.sub_types(false, &obligation.cause, a, b)
+ self.infcx.eq_types(false, &obligation.cause, a, b)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);
}
});
let new_struct = tcx.mk_adt(def, tcx.mk_substs(params));
let InferOk { obligations, .. } =
- self.infcx.sub_types(false, &obligation.cause, new_struct, target)
+ self.infcx.eq_types(false, &obligation.cause, new_struct, target)
.map_err(|_| Unimplemented)?;
self.inferred_obligations.extend(obligations);
pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator>;
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
- tcx.dep_graph.with_task(DepNode::BorrowCheckKrate, || {
+ tcx.dep_graph.with_task(DepNode::BorrowCheckKrate, tcx, (), check_crate_task);
+
+ fn check_crate_task<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, (): ()) {
tcx.visit_all_bodies_in_krate(|body_owner_def_id, body_id| {
- tcx.dep_graph.with_task(DepNode::BorrowCheck(body_owner_def_id), || {
- borrowck_fn(tcx, body_id);
- });
+ tcx.dep_graph.with_task(DepNode::BorrowCheck(body_owner_def_id),
+ tcx,
+ body_id,
+ borrowck_fn);
});
- });
+ }
}
/// Collection of conclusions determined via borrow checker analyses.
use serialize::json;
use std::env;
-use std::mem;
use std::ffi::{OsString, OsStr};
use std::fs;
use std::io::{self, Write};
krate
});
- krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());
-
krate = time(time_passes, "maybe building test harness", || {
syntax::test::modify_for_testing(&sess.parse_sess,
&mut resolver,
const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0",
"sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0",
"ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0",
- "sse4a\0", "rdrnd\0", "rdseed\0"];
+ "sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"];
/// Add `target_feature = "..."` cfgs for a variety of platform
/// specific features (SSE, NEON etc.).
clean_work_products.insert(wp.clone());
}
- tcx.dep_graph.with_task(n, || ()); // create the node with no inputs
+ tcx.dep_graph.with_task(n, (), (), create_node);
+
+ fn create_node((): (), (): ()) {
+ // just create the node with no inputs
+ }
}
}
sess.imported_macro_spans.borrow_mut()
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
- LoadedMacro::MacroRules(ast::MacroDef {
+ LoadedMacro::MacroDef(ast::Item {
ident: ast::Ident::with_empty_ctxt(name),
id: ast::DUMMY_NODE_ID,
span: local_span,
attrs: attrs,
- body: body.into(),
+ node: ast::ItemKind::MacroDef(body.into()),
+ vis: ast::Visibility::Inherited,
})
}
(https://github.com/rust-lang/rust/issues/39283)");
}
- if temp_lifetime.is_some() {
+ if !expr_ty.is_never() && temp_lifetime.is_some() {
this.cfg.push(block, Statement {
source_info: source_info,
kind: StatementKind::StorageLive(temp.clone())
use std::mem;
pub fn build_mir_for_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
- tcx.dep_graph.with_task(DepNode::MirKrate, || {
+ tcx.dep_graph.with_task(DepNode::MirKrate, tcx, (), build_mir_for_crate_task);
+
+ fn build_mir_for_crate_task<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, (): ()) {
tcx.visit_all_bodies_in_krate(|body_owner_def_id, _body_id| {
tcx.item_mir(body_owner_def_id);
});
- });
+ }
}
pub fn provide(providers: &mut Providers) {
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
self.record("Attribute", Id::None, attr);
}
-
- fn visit_macro_def(&mut self, macro_def: &'v ast::MacroDef) {
- self.record("MacroDef", Id::None, macro_def);
- ast_visit::walk_macro_def(self, macro_def)
- }
}
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::ext::base::SyntaxExtension;
use syntax::ext::base::Determinacy::Undetermined;
-use syntax::ext::expand::mark_tts;
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
use syntax::parse::token;
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.current_module = module;
}
- ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
+ ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
}
}
})
}
+ pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
+ let def_id = self.macro_defs[&expansion];
+ if let Some(id) = self.definitions.as_local_node_id(def_id) {
+ self.local_macro_def_scopes[&id]
+ } else {
+ let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
+ self.get_extern_crate_root(module_def_id.krate)
+ }
+ }
+
pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
let def_id = match def {
Def::Macro(def_id, ..) => def_id,
return ext.clone();
}
- let mut macro_rules = match self.session.cstore.load_macro(def_id, &self.session) {
- LoadedMacro::MacroRules(macro_rules) => macro_rules,
+ let macro_def = match self.session.cstore.load_macro(def_id, &self.session) {
+ LoadedMacro::MacroDef(macro_def) => macro_def,
LoadedMacro::ProcMacro(ext) => return ext,
};
- let mark = Mark::fresh();
- let invocation = self.arenas.alloc_invocation_data(InvocationData {
- module: Cell::new(self.get_extern_crate_root(def_id.krate)),
- def_index: CRATE_DEF_INDEX,
- const_expr: false,
- legacy_scope: Cell::new(LegacyScope::Empty),
- expansion: Cell::new(LegacyScope::Empty),
- });
- self.invocations.insert(mark, invocation);
- macro_rules.body = mark_tts(macro_rules.stream(), mark).into();
- let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, ¯o_rules));
+ let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, ¯o_def));
self.macro_map.insert(def_id, ext.clone());
ext
}
fn visit_item(&mut self, item: &'a Item) {
let macro_use = match item.node {
- ItemKind::Mac(ref mac) => {
- if mac.node.path.segments.is_empty() {
- self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
- } else {
- self.resolver.define_macro(item, &mut self.legacy_scope);
- }
+ ItemKind::MacroDef(..) => {
+ self.resolver.define_macro(item, &mut self.legacy_scope);
+ return
+ }
+ ItemKind::Mac(..) => {
+ self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
return
}
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
// We passed through a module.
ModuleRibKind(Module<'a>),
- // We passed through a `macro_rules!` statement with the given expansion
- MacroDefinition(Mark),
+ // We passed through a `macro_rules!` statement
+ MacroDefinition(DefId),
// All bindings in this rib are type parameters that can't be used
// from the default of a type parameter because they're not declared
}
}
- fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
+ fn def_ignoring_ambiguity(&self) -> Def {
match self.kind {
- NameBindingKind::Import { binding, .. } => binding.get_macro(resolver),
- NameBindingKind::Ambiguity { b1, .. } => b1.get_macro(resolver),
- _ => resolver.get_macro(self.def()),
+ NameBindingKind::Import { binding, .. } => binding.def_ignoring_ambiguity(),
+ NameBindingKind::Ambiguity { b1, .. } => b1.def_ignoring_ambiguity(),
+ _ => self.def(),
}
}
+ fn get_macro(&self, resolver: &mut Resolver<'a>) -> Rc<SyntaxExtension> {
+ resolver.get_macro(self.def_ignoring_ambiguity())
+ }
+
// We sometimes need to treat variants as `pub` for backwards compatibility
fn pseudo_vis(&self) -> ty::Visibility {
if self.is_variant() { ty::Visibility::Public } else { self.vis }
pub definitions: Definitions,
- // Maps the node id of a statement to the expansions of the `macro_rules!`s
- // immediately above the statement (if appropriate).
- macros_at_scope: FxHashMap<NodeId, Vec<Mark>>,
-
graph_root: Module<'a>,
prelude: Option<Module<'a>>,
dummy_binding: &'a NameBinding<'a>,
use_extern_macros: bool, // true if `#![feature(use_extern_macros)]`
- pub exported_macros: Vec<ast::MacroDef>,
crate_loader: &'a mut CrateLoader,
macro_names: FxHashSet<Name>,
builtin_macros: FxHashMap<Name, &'a NameBinding<'a>>,
lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
+ macro_defs: FxHashMap<Mark, DefId>,
+ local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
macro_exports: Vec<Export>,
pub whitelisted_legacy_custom_derives: Vec<Name>,
pub found_unresolved_macro: bool,
let features = session.features.borrow();
+ let mut macro_defs = FxHashMap();
+ macro_defs.insert(Mark::root(), root_def_id);
+
Resolver {
session: session,
definitions: definitions,
- macros_at_scope: FxHashMap(),
// The outermost module has def ID 0; this is not reflected in the
// AST.
// `#![feature(proc_macro)]` implies `#[feature(extern_macros)]`
use_extern_macros: features.use_extern_macros || features.proc_macro,
- exported_macros: Vec::new(),
crate_loader: crate_loader,
macro_names: FxHashSet(),
builtin_macros: FxHashMap(),
macro_map: FxHashMap(),
macro_exports: Vec::new(),
invocations: invocations,
+ macro_defs: macro_defs,
+ local_macro_def_scopes: FxHashMap(),
name_already_seen: FxHashMap(),
whitelisted_legacy_custom_derives: Vec::new(),
proc_macro_enabled: features.proc_macro,
}
}
- if let MacroDefinition(mac) = self.ribs[ns][i].kind {
+ if let MacroDefinition(def) = self.ribs[ns][i].kind {
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
- let (source_ctxt, source_macro) = ident.ctxt.source();
- if source_macro == mac {
- ident.ctxt = source_ctxt;
+ let ctxt_data = ident.ctxt.data();
+ if def == self.macro_defs[&ctxt_data.outer_mark] {
+ ident.ctxt = ctxt_data.prev_ctxt;
}
}
}
None
}
- fn resolve_crate_var(&mut self, mut crate_var_ctxt: SyntaxContext) -> Module<'a> {
- while crate_var_ctxt.source().0 != SyntaxContext::empty() {
- crate_var_ctxt = crate_var_ctxt.source().0;
+ fn resolve_crate_var(&mut self, crate_var_ctxt: SyntaxContext) -> Module<'a> {
+ let mut ctxt_data = crate_var_ctxt.data();
+ while ctxt_data.prev_ctxt != SyntaxContext::empty() {
+ ctxt_data = ctxt_data.prev_ctxt.data();
}
- let module = self.invocations[&crate_var_ctxt.source().1].module.get();
+ let module = self.macro_def_scope(ctxt_data.outer_mark);
if module.is_local() { self.graph_root } else { module }
}
NormalRibKind => {
// Continue
}
- MacroDefinition(mac) => {
+ MacroDefinition(def) => {
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
- let (source_ctxt, source_macro) = ident.ctxt.source();
- if source_macro == mac {
- ident.ctxt = source_ctxt;
+ let ctxt_data = ident.ctxt.data();
+ if def == self.macro_defs[&ctxt_data.outer_mark] {
+ ident.ctxt = ctxt_data.prev_ctxt;
}
}
_ => {
}
}
- ItemKind::ExternCrate(_) => {
+ ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) => {
// do nothing, these are just around to be encoded
}
// Descend into the block.
for stmt in &block.stmts {
- if let Some(marks) = self.macros_at_scope.remove(&stmt.id) {
- num_macro_definition_ribs += marks.len() as u32;
- for mark in marks {
- self.ribs[ValueNS].push(Rib::new(MacroDefinition(mark)));
- self.label_ribs.push(Rib::new(MacroDefinition(mark)));
+ if let ast::StmtKind::Item(ref item) = stmt.node {
+ if let ast::ItemKind::MacroDef(..) = item.node {
+ num_macro_definition_ribs += 1;
+ let def = self.definitions.local_def_id(item.id);
+ self.ribs[ValueNS].push(Rib::new(MacroDefinition(def)));
+ self.label_ribs.push(Rib::new(MacroDefinition(def)));
}
}
use rustc::hir::def::{Def, Export};
use rustc::hir::map::{self, DefCollector};
use rustc::ty;
-use std::cell::Cell;
-use std::rc::Rc;
use syntax::ast::{self, Name, Ident};
-use syntax::attr;
+use syntax::attr::{self, HasAttrs};
use syntax::errors::DiagnosticBuilder;
-use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator};
-use syntax::ext::base::{Resolver as SyntaxResolver, SyntaxExtension};
-use syntax::ext::base::MacroKind;
-use syntax::ext::expand::{Expansion, mark_tts};
+use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
+use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
+use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
use syntax::ext::hygiene::Mark;
+use syntax::ext::placeholders::placeholder;
use syntax::ext::tt::macro_rules;
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
use syntax::fold::{self, Folder};
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::util::lev_distance::find_best_match_for_name;
-use syntax::visit::Visitor;
use syntax_pos::{Span, DUMMY_SP};
+use std::cell::Cell;
+use std::mem;
+use std::rc::Rc;
+
#[derive(Clone)]
pub struct InvocationData<'a> {
pub module: Cell<Module<'a>>,
pub struct LegacyBinding<'a> {
pub parent: Cell<LegacyScope<'a>>,
pub name: ast::Name,
- ext: Rc<SyntaxExtension>,
+ def_id: DefId,
pub span: Span,
}
invocation.expansion.set(visitor.legacy_scope);
}
- fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
+ fn add_builtin(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
let def_id = DefId {
krate: BUILTIN_MACROS_CRATE,
index: DefIndex::new(self.macro_map.len()),
self.builtin_macros.insert(ident.name, binding);
}
- fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>) {
- self.macros_at_scope.insert(id, macros);
- }
-
fn resolve_imports(&mut self) {
ImportResolver { resolver: self }.resolve_imports()
}
None
}
- fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind,
- force: bool) -> Result<Rc<SyntaxExtension>, Determinacy> {
+ fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
+ -> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
+ let def = match invoc.kind {
+ InvocationKind::Attr { attr: None, .. } => return Ok(None),
+ _ => match self.resolve_invoc_to_def(invoc, scope, force) {
+ Ok(def) => def,
+ Err(determinacy) => return Err(determinacy),
+ },
+ };
+ self.macro_defs.insert(invoc.expansion_data.mark, def.def_id());
+ Ok(Some(self.get_macro(def)))
+ }
+
+ fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
+ -> Result<Rc<SyntaxExtension>, Determinacy> {
+ self.resolve_macro_to_def(scope, path, kind, force).map(|def| self.get_macro(def))
+ }
+}
+
+impl<'a> Resolver<'a> {
+ fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
+ -> Result<Def, Determinacy> {
+ let (attr, traits, item) = match invoc.kind {
+ InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
+ InvocationKind::Bang { ref mac, .. } => {
+ return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
+ }
+ InvocationKind::Derive { name, span, .. } => {
+ let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
+ return self.resolve_macro_to_def(scope, &path, MacroKind::Derive, force);
+ }
+ };
+
+ let (attr_name, path) = {
+ let attr = attr.as_ref().unwrap();
+ (attr.name(), ast::Path::from_ident(attr.span, Ident::with_empty_ctxt(attr.name())))
+ };
+
+ let mut determined = true;
+ match self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force) {
+ Ok(def) => return Ok(def),
+ Err(Determinacy::Undetermined) => determined = false,
+ Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
+ Err(Determinacy::Determined) => {}
+ }
+
+ for &(name, span) in traits {
+ let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
+ match self.resolve_macro(scope, &path, MacroKind::Derive, force) {
+ Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs) = *ext {
+ if inert_attrs.contains(&attr_name) {
+ // FIXME(jseyfried) Avoid `mem::replace` here.
+ let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
+ .make_items().pop().unwrap();
+ let dummy_item = Annotatable::Item(dummy_item);
+ *item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
+ let inert_attr = attr.take().unwrap();
+ attr::mark_known(&inert_attr);
+ if self.proc_macro_enabled {
+ *attr = find_attr_invoc(&mut attrs);
+ }
+ attrs.push(inert_attr);
+ attrs
+ });
+ }
+ return Err(Determinacy::Undetermined);
+ },
+ Err(Determinacy::Undetermined) => determined = false,
+ Err(Determinacy::Determined) => {}
+ }
+ }
+
+ Err(if determined { Determinacy::Determined } else { Determinacy::Undetermined })
+ }
+
+ fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
+ -> Result<Def, Determinacy> {
let ast::Path { ref segments, span } = *path;
if segments.iter().any(|segment| segment.parameters.is_some()) {
let kind =
return Err(Determinacy::Determined);
}
- let ext = match self.resolve_path(&path, Some(MacroNS), None) {
+ let def = match self.resolve_path(&path, Some(MacroNS), None) {
PathResult::NonModule(path_res) => match path_res.base_def() {
Def::Err => Err(Determinacy::Determined),
- def @ _ => Ok(self.get_macro(def)),
+ def @ _ => Ok(def),
},
PathResult::Module(..) => unreachable!(),
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
};
self.current_module.macro_resolutions.borrow_mut()
.push((path.into_boxed_slice(), span));
- return ext;
+ return def;
}
let name = path[0].name;
let result = match self.resolve_legacy_scope(&invocation.legacy_scope, name, false) {
- Some(MacroBinding::Legacy(binding)) => Ok(binding.ext.clone()),
- Some(MacroBinding::Modern(binding)) => Ok(binding.get_macro(self)),
+ Some(MacroBinding::Legacy(binding)) => Ok(Def::Macro(binding.def_id, MacroKind::Bang)),
+ Some(MacroBinding::Modern(binding)) => Ok(binding.def_ignoring_ambiguity()),
None => match self.resolve_lexical_macro_path_segment(path[0], MacroNS, None) {
- Ok(binding) => Ok(binding.get_macro(self)),
+ Ok(binding) => Ok(binding.def_ignoring_ambiguity()),
Err(Determinacy::Undetermined) if !force =>
return Err(Determinacy::Undetermined),
Err(_) => {
result
}
-}
-impl<'a> Resolver<'a> {
// Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
pub fn resolve_lexical_macro_path_segment(&mut self,
ident: Ident,
}
pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
- let tts = match item.node {
- ast::ItemKind::Mac(ref mac) => mac.node.stream(),
- _ => unreachable!(),
- };
-
- if item.ident.name == "macro_rules" {
+ self.local_macro_def_scopes.insert(item.id, self.current_module);
+ let ident = item.ident;
+ if ident.name == "macro_rules" {
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
}
- let mark = Mark::from_placeholder_id(item.id);
- let invocation = self.invocations[&mark];
- invocation.module.set(self.current_module);
-
- let mut def = ast::MacroDef {
- ident: item.ident,
- attrs: item.attrs.clone(),
- id: ast::DUMMY_NODE_ID,
- span: item.span,
- body: mark_tts(tts, mark).into(),
- };
-
+ let def_id = self.definitions.local_def_id(item.id);
+ let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, item));
+ self.macro_map.insert(def_id, ext);
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
- parent: Cell::new(*legacy_scope),
- name: def.ident.name,
- ext: Rc::new(macro_rules::compile(&self.session.parse_sess, &def)),
- span: def.span,
+ parent: Cell::new(*legacy_scope), name: ident.name, def_id: def_id, span: item.span,
}));
- self.macro_names.insert(def.ident.name);
+ self.macro_names.insert(ident.name);
- if attr::contains_name(&def.attrs, "macro_export") {
- def.id = self.next_node_id();
- DefCollector::new(&mut self.definitions).with_parent(CRATE_DEF_INDEX, |collector| {
- collector.visit_macro_def(&def)
- });
- self.macro_exports.push(Export {
- name: def.ident.name,
- def: Def::Macro(self.definitions.local_def_id(def.id), MacroKind::Bang),
- });
- self.exported_macros.push(def);
+ if attr::contains_name(&item.attrs, "macro_export") {
+ let def = Def::Macro(def_id, MacroKind::Bang);
+ self.macro_exports.push(Export { name: ident.name, def: def });
}
}
}
}
None => {
- span_bug!(span, "Could not find container for method {}", id);
+ debug!("Could not find container for method {} at {:?}", id, span);
+ // This is not necessarily a bug, if there was a compilation error, the tables
+ // we need might not exist.
+ return None;
}
},
};
use rustc::traits;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::adjustment::CustomCoerceUnsized;
-use rustc::dep_graph::{DepNode, WorkProduct};
+use rustc::dep_graph::{AssertDepGraphSafe, DepNode, WorkProduct};
use rustc::hir::map as hir_map;
use rustc::util::common::time;
use session::config::{self, NoDebugInfo};
// Instantiate translation items without filling out definitions yet...
for ccx in crate_context_list.iter_need_trans() {
- let cgu = ccx.codegen_unit();
- let trans_items = cgu.items_in_deterministic_order(tcx, &symbol_map);
-
- tcx.dep_graph.with_task(cgu.work_product_dep_node(), || {
+ let dep_node = ccx.codegen_unit().work_product_dep_node();
+ tcx.dep_graph.with_task(dep_node,
+ ccx,
+ AssertDepGraphSafe(symbol_map.clone()),
+ trans_decl_task);
+
+ fn trans_decl_task<'a, 'tcx>(ccx: CrateContext<'a, 'tcx>,
+ symbol_map: AssertDepGraphSafe<Rc<SymbolMap<'tcx>>>) {
+ // FIXME(#40304): Instead of this, the symbol-map should be an
+ // on-demand thing that we compute.
+ let AssertDepGraphSafe(symbol_map) = symbol_map;
+ let cgu = ccx.codegen_unit();
+ let trans_items = cgu.items_in_deterministic_order(ccx.tcx(), &symbol_map);
for (trans_item, linkage) in trans_items {
trans_item.predefine(&ccx, linkage);
}
- });
+ }
}
// ... and now that we have everything pre-defined, fill out those definitions.
for ccx in crate_context_list.iter_need_trans() {
- let cgu = ccx.codegen_unit();
- let trans_items = cgu.items_in_deterministic_order(tcx, &symbol_map);
- tcx.dep_graph.with_task(cgu.work_product_dep_node(), || {
+ let dep_node = ccx.codegen_unit().work_product_dep_node();
+ tcx.dep_graph.with_task(dep_node,
+ ccx,
+ AssertDepGraphSafe(symbol_map.clone()),
+ trans_def_task);
+
+ fn trans_def_task<'a, 'tcx>(ccx: CrateContext<'a, 'tcx>,
+ symbol_map: AssertDepGraphSafe<Rc<SymbolMap<'tcx>>>) {
+ // FIXME(#40304): Instead of this, the symbol-map should be an
+ // on-demand thing that we compute.
+ let AssertDepGraphSafe(symbol_map) = symbol_map;
+ let cgu = ccx.codegen_unit();
+ let trans_items = cgu.items_in_deterministic_order(ccx.tcx(), &symbol_map);
for (trans_item, _) in trans_items {
trans_item.define(&ccx);
}
if ccx.sess().opts.debuginfo != NoDebugInfo {
debuginfo::finalize(&ccx);
}
- });
+ }
}
symbol_names_test::report_symbol_names(&shared_ccx);
use llvm;
use llvm::{ContextRef, ModuleRef, ValueRef};
-use rustc::dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig, WorkProduct};
+use rustc::dep_graph::{DepGraph, DepGraphSafe, DepNode, DepTrackingMap,
+ DepTrackingMapConfig, WorkProduct};
use middle::cstore::LinkMeta;
use rustc::hir;
use rustc::hir::def::ExportMap;
index: usize,
}
+impl<'a, 'tcx> DepGraphSafe for CrateContext<'a, 'tcx> {
+}
+
pub struct CrateContextIterator<'a, 'tcx: 'a> {
shared: &'a SharedCrateContext<'a, 'tcx>,
local_ccxs: &'a [LocalCrateContext<'tcx>],
use super::FnCtxt;
+use rustc::infer::InferOk;
use rustc::traits;
use rustc::ty::{self, Ty, TraitRef};
use rustc::ty::{ToPredicate, TypeFoldable};
pub fn finalize<'b, I>(self, pref: LvaluePreference, exprs: I)
where I: IntoIterator<Item = &'b hir::Expr>
+ {
+ let fcx = self.fcx;
+ fcx.register_infer_ok_obligations(self.finalize_as_infer_ok(pref, exprs));
+ }
+
+ pub fn finalize_as_infer_ok<'b, I>(self, pref: LvaluePreference, exprs: I)
+ -> InferOk<'tcx, ()>
+ where I: IntoIterator<Item = &'b hir::Expr>
{
let methods: Vec<_> = self.steps
.iter()
}
}
- for obligation in self.obligations {
- self.fcx.register_predicate(obligation);
+ InferOk {
+ value: (),
+ obligations: self.obligations
}
}
}
use rustc::hir;
use rustc::hir::def_id::DefId;
-use rustc::infer::{Coercion, InferOk, TypeTrace};
+use rustc::infer::{Coercion, InferResult, InferOk, TypeTrace};
+use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::traits::{self, ObligationCause, ObligationCauseCode};
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
use rustc::ty::{self, LvaluePreference, TypeAndMut,
use rustc::ty::subst::Subst;
use syntax::abi;
use syntax::feature_gate;
-use util::common::indent;
-use std::cell::RefCell;
use std::collections::VecDeque;
use std::ops::Deref;
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
cause: ObligationCause<'tcx>,
use_lub: bool,
- unsizing_obligations: RefCell<Vec<traits::PredicateObligation<'tcx>>>,
}
impl<'a, 'gcx, 'tcx> Deref for Coerce<'a, 'gcx, 'tcx> {
}
}
-type CoerceResult<'tcx> = RelateResult<'tcx, (Ty<'tcx>, Adjust<'tcx>)>;
+type CoerceResult<'tcx> = InferResult<'tcx, Adjustment<'tcx>>;
fn coerce_mutbls<'tcx>(from_mutbl: hir::Mutability,
to_mutbl: hir::Mutability)
}
}
+fn identity<'tcx>() -> Adjust<'tcx> {
+ Adjust::DerefRef {
+ autoderefs: 0,
+ autoref: None,
+ unsize: false,
+ }
+}
+
+fn success<'tcx>(kind: Adjust<'tcx>,
+ target: Ty<'tcx>,
+ obligations: traits::PredicateObligations<'tcx>)
+ -> CoerceResult<'tcx> {
+ Ok(InferOk {
+ value: Adjustment {
+ kind,
+ target
+ },
+ obligations
+ })
+}
+
impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
fn new(fcx: &'f FnCtxt<'f, 'gcx, 'tcx>, cause: ObligationCause<'tcx>) -> Self {
Coerce {
fcx: fcx,
cause: cause,
use_lub: false,
- unsizing_obligations: RefCell::new(vec![]),
}
}
- fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
+ fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
self.commit_if_ok(|_| {
let trace = TypeTrace::types(&self.cause, false, a, b);
if self.use_lub {
self.lub(false, trace, &a, &b)
- .map(|ok| self.register_infer_ok_obligations(ok))
} else {
self.sub(false, trace, &a, &b)
- .map(|InferOk { value, obligations }| {
- self.fcx.register_predicates(obligations);
- value
- })
}
})
}
- /// Unify two types (using sub or lub) and produce a noop coercion.
- fn unify_and_identity(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
- self.unify(&a, &b).and_then(|ty| self.identity(ty))
- }
-
- /// Synthesize an identity adjustment.
- fn identity(&self, ty: Ty<'tcx>) -> CoerceResult<'tcx> {
- Ok((ty, Adjust::DerefRef {
- autoderefs: 0,
- autoref: None,
- unsize: false,
- }))
+ /// Unify two types (using sub or lub) and produce a specific coercion.
+ fn unify_and(&self, a: Ty<'tcx>, b: Ty<'tcx>, kind: Adjust<'tcx>)
+ -> CoerceResult<'tcx> {
+ self.unify(&a, &b).and_then(|InferOk { value: ty, obligations }| {
+ success(kind, ty, obligations)
+ })
}
fn coerce<'a, E, I>(&self, exprs: &E, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx>
// Just ignore error types.
if a.references_error() || b.references_error() {
- return self.identity(b);
+ return success(identity(), b, vec![]);
}
if a.is_never() {
- return Ok((b, Adjust::NeverToAny));
+ return success(Adjust::NeverToAny, b, vec![]);
}
// Consider coercing the subtype to a DST
}
_ => {
// Otherwise, just use unification rules.
- self.unify_and_identity(a, b)
+ self.unify_and(a, b, identity())
}
}
}
coerce_mutbls(mt_a.mutbl, mt_b.mutbl)?;
(r_a, mt_a)
}
- _ => return self.unify_and_identity(a, b),
+ _ => return self.unify_and(a, b, identity()),
};
let span = self.cause.span;
let mut first_error = None;
let mut r_borrow_var = None;
let mut autoderef = self.autoderef(span, a);
- let mut success = None;
+ let mut found = None;
for (referent_ty, autoderefs) in autoderef.by_ref() {
if autoderefs == 0 {
mutbl: mt_b.mutbl, // [1] above
});
match self.unify(derefd_ty_a, b) {
- Ok(ty) => {
- success = Some((ty, autoderefs));
+ Ok(ok) => {
+ found = Some((ok, autoderefs));
break;
}
Err(err) => {
// (e.g., in example above, the failure from relating `Vec<T>`
// to the target type), since that should be the least
// confusing.
- let (ty, autoderefs) = match success {
+ let (InferOk { value: ty, mut obligations }, autoderefs) = match found {
Some(d) => d,
None => {
let err = first_error.expect("coerce_borrowed_pointer had no error");
}
};
- // This commits the obligations to the fulfillcx. After this succeeds,
- // this snapshot can't be rolled back.
- autoderef.finalize(LvaluePreference::from_mutbl(mt_b.mutbl), exprs());
-
- // Now apply the autoref. We have to extract the region out of
- // the final ref type we got.
if ty == a && mt_a.mutbl == hir::MutImmutable && autoderefs == 1 {
// As a special case, if we would produce `&'a *x`, that's
// a total no-op. We end up with the type `&'a T` just as
// `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
// which is a borrow.
assert_eq!(mt_b.mutbl, hir::MutImmutable); // can only coerce &T -> &U
- return self.identity(ty);
+ return success(identity(), ty, obligations);
}
+
+ // Now apply the autoref. We have to extract the region out of
+ // the final ref type we got.
let r_borrow = match ty.sty {
ty::TyRef(r_borrow, _) => r_borrow,
_ => span_bug!(span, "expected a ref type, got {:?}", ty),
ty,
autoderefs,
autoref);
- Ok((ty, Adjust::DerefRef {
+
+ let pref = LvaluePreference::from_mutbl(mt_b.mutbl);
+ obligations.extend(autoderef.finalize_as_infer_ok(pref, exprs()).obligations);
+
+ success(Adjust::DerefRef {
autoderefs: autoderefs,
autoref: autoref,
unsize: false,
- }))
+ }, ty, obligations)
}
}
_ => (source, None),
};
- let source = source.adjust_for_autoref(self.tcx, reborrow);
+ let coerce_source = source.adjust_for_autoref(self.tcx, reborrow);
+
+ let adjust = Adjust::DerefRef {
+ autoderefs: if reborrow.is_some() { 1 } else { 0 },
+ autoref: reborrow,
+ unsize: true,
+ };
+
+ // Setup either a subtyping or a LUB relationship between
+ // the `CoerceUnsized` target type and the expected type.
+ // We only have the latter, so we use an inference variable
+ // for the former and let type inference do the rest.
+ let origin = TypeVariableOrigin::MiscVariable(self.cause.span);
+ let coerce_target = self.next_ty_var(origin);
+ let mut coercion = self.unify_and(coerce_target, target, adjust)?;
let mut selcx = traits::SelectionContext::new(self);
// Use a FIFO queue for this custom fulfillment procedure.
let mut queue = VecDeque::new();
- let mut leftover_predicates = vec![];
// Create an obligation for `Source: CoerceUnsized<Target>`.
let cause = ObligationCause::misc(self.cause.span, self.body_id);
queue.push_back(self.tcx
- .predicate_for_trait_def(cause, coerce_unsized_did, 0, source, &[target]));
+ .predicate_for_trait_def(cause, coerce_unsized_did, 0,
+ coerce_source, &[coerce_target]));
// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
// emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
let trait_ref = match obligation.predicate {
ty::Predicate::Trait(ref tr) if traits.contains(&tr.def_id()) => tr.clone(),
_ => {
- leftover_predicates.push(obligation);
+ coercion.obligations.push(obligation);
continue;
}
};
}
}
- *self.unsizing_obligations.borrow_mut() = leftover_predicates;
-
- let adjustment = Adjust::DerefRef {
- autoderefs: if reborrow.is_some() { 1 } else { 0 },
- autoref: reborrow,
- unsize: true,
- };
- debug!("Success, coerced with {:?}", adjustment);
- Ok((target, adjustment))
+ Ok(coercion)
}
fn coerce_from_safe_fn(&self,
a: Ty<'tcx>,
fn_ty_a: ty::PolyFnSig<'tcx>,
- b: Ty<'tcx>)
+ b: Ty<'tcx>,
+ to_unsafe: Adjust<'tcx>,
+ normal: Adjust<'tcx>)
-> CoerceResult<'tcx> {
if let ty::TyFnPtr(fn_ty_b) = b.sty {
match (fn_ty_a.unsafety(), fn_ty_b.unsafety()) {
(hir::Unsafety::Normal, hir::Unsafety::Unsafe) => {
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
- return self.unify_and_identity(unsafe_a, b)
- .map(|(ty, _)| (ty, Adjust::UnsafeFnPointer));
+ return self.unify_and(unsafe_a, b, to_unsafe);
}
_ => {}
}
}
- self.unify_and_identity(a, b)
+ self.unify_and(a, b, normal)
}
fn coerce_from_fn_pointer(&self,
let b = self.shallow_resolve(b);
debug!("coerce_from_fn_pointer(a={:?}, b={:?})", a, b);
- self.coerce_from_safe_fn(a, fn_ty_a, b)
+ self.coerce_from_safe_fn(a, fn_ty_a, b,
+ Adjust::UnsafeFnPointer, identity())
}
fn coerce_from_fn_item(&self,
match b.sty {
ty::TyFnPtr(_) => {
let a_fn_pointer = self.tcx.mk_fn_ptr(fn_ty_a);
- self.coerce_from_safe_fn(a_fn_pointer, fn_ty_a, b)
- .map(|(ty, _)| (ty, Adjust::ReifyFnPointer))
+ self.coerce_from_safe_fn(a_fn_pointer, fn_ty_a, b,
+ Adjust::ReifyFnPointer, Adjust::ReifyFnPointer)
}
- _ => self.unify_and_identity(a, b),
+ _ => self.unify_and(a, b, identity()),
}
}
self.cause.span,
feature_gate::GateIssue::Language,
feature_gate::CLOSURE_TO_FN_COERCION);
- return self.unify_and_identity(a, b);
+ return self.unify_and(a, b, identity());
}
// We coerce the closure, which has fn type
// `extern "rust-call" fn((arg0,arg1,...)) -> _`
let pointer_ty = self.tcx.mk_fn_ptr(converted_sig);
debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})",
a, b, pointer_ty);
- self.unify_and_identity(pointer_ty, b)
- .map(|(ty, _)| (ty, Adjust::ClosureFnPointer))
+ self.unify_and(pointer_ty, b, Adjust::ClosureFnPointer)
}
- _ => self.unify_and_identity(a, b),
+ _ => self.unify_and(a, b, identity()),
}
}
ty::TyRef(_, mt) => (true, mt),
ty::TyRawPtr(mt) => (false, mt),
_ => {
- return self.unify_and_identity(a, b);
+ return self.unify_and(a, b, identity());
}
};
mutbl: mutbl_b,
ty: mt_a.ty,
});
- let (ty, noop) = self.unify_and_identity(a_unsafe, b)?;
coerce_mutbls(mt_a.mutbl, mutbl_b)?;
-
// Although references and unsafe ptrs have the same
// representation, we still register an Adjust::DerefRef so that
// regionck knows that the region for `a` must be valid here.
- Ok((ty,
- if is_ref {
- Adjust::DerefRef {
- autoderefs: 1,
- autoref: Some(AutoBorrow::RawPtr(mutbl_b)),
- unsize: false,
- }
- } else if mt_a.mutbl != mutbl_b {
- Adjust::MutToConstPointer
- } else {
- noop
- }))
- }
-}
-
-fn apply<'a, 'b, 'gcx, 'tcx, E, I>(coerce: &mut Coerce<'a, 'gcx, 'tcx>,
- exprs: &E,
- a: Ty<'tcx>,
- b: Ty<'tcx>)
- -> RelateResult<'tcx, Adjustment<'tcx>>
- where E: Fn() -> I,
- I: IntoIterator<Item = &'b hir::Expr>
-{
-
- let (ty, adjust) = indent(|| coerce.coerce(exprs, a, b))?;
-
- let fcx = coerce.fcx;
- if let Adjust::DerefRef { unsize: true, .. } = adjust {
- let mut obligations = coerce.unsizing_obligations.borrow_mut();
- for obligation in obligations.drain(..) {
- fcx.register_predicate(obligation);
- }
+ self.unify_and(a_unsafe, b, if is_ref {
+ Adjust::DerefRef {
+ autoderefs: 1,
+ autoref: Some(AutoBorrow::RawPtr(mutbl_b)),
+ unsize: false,
+ }
+ } else if mt_a.mutbl != mutbl_b {
+ Adjust::MutToConstPointer
+ } else {
+ identity()
+ })
}
-
- Ok(Adjustment {
- kind: adjust,
- target: ty
- })
}
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
let cause = self.cause(expr.span, ObligationCauseCode::ExprAssignable);
- let mut coerce = Coerce::new(self, cause);
+ let coerce = Coerce::new(self, cause);
self.commit_if_ok(|_| {
- let adjustment = apply(&mut coerce, &|| Some(expr), source, target)?;
+ let ok = coerce.coerce(&|| Some(expr), source, target)?;
+ let adjustment = self.register_infer_ok_obligations(ok);
if !adjustment.is_identity() {
debug!("Success, coerced with {:?}", adjustment);
match self.tables.borrow().adjustments.get(&expr.id) {
// but only if the new expression has no coercion already applied to it.
let mut first_error = None;
if !self.tables.borrow().adjustments.contains_key(&new.id) {
- let result = self.commit_if_ok(|_| apply(&mut coerce, &|| Some(new), new_ty, prev_ty));
+ let result = self.commit_if_ok(|_| coerce.coerce(&|| Some(new), new_ty, prev_ty));
match result {
- Ok(adjustment) => {
+ Ok(ok) => {
+ let adjustment = self.register_infer_ok_obligations(ok);
if !adjustment.is_identity() {
self.write_adjustment(new.id, adjustment);
}
}
}
- match self.commit_if_ok(|_| apply(&mut coerce, &exprs, prev_ty, new_ty)) {
+ match self.commit_if_ok(|_| coerce.coerce(&exprs, prev_ty, new_ty)) {
Err(_) => {
// Avoid giving strange errors on failed attempts.
if let Some(e) = first_error {
})
}
}
- Ok(adjustment) => {
+ Ok(ok) => {
+ let adjustment = self.register_infer_ok_obligations(ok);
if !adjustment.is_identity() {
let mut tables = self.tables.borrow_mut();
for expr in exprs() {
}
pub fn check_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CompileResult {
- tcx.sess.track_errors(|| {
- tcx.dep_graph.with_task(DepNode::TypeckBodiesKrate, || {
- tcx.visit_all_bodies_in_krate(|body_owner_def_id, _body_id| {
- tcx.item_tables(body_owner_def_id);
- });
+ return tcx.sess.track_errors(|| {
+ tcx.dep_graph.with_task(DepNode::TypeckBodiesKrate, tcx, (), check_item_bodies_task);
+ });
+
+ fn check_item_bodies_task<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, (): ()) {
+ tcx.visit_all_bodies_in_krate(|body_owner_def_id, _body_id| {
+ tcx.item_tables(body_owner_def_id);
});
- })
+ }
}
pub fn provide(providers: &mut Providers) {
/// 4. This is added by the code in `visit_expr` when we write to `item_types`.
/// 5. This is added by the code in `convert_item` when we write to `item_types`;
/// note that this write occurs inside the `CollectItemSig` task.
- /// 6. Added by explicit `read` below
- fn with_collect_item_sig<OP>(&self, id: ast::NodeId, op: OP)
- where OP: FnOnce()
- {
+ /// 6. Added by reads from within `op`.
+ fn with_collect_item_sig(&self, id: ast::NodeId, op: fn(TyCtxt<'a, 'tcx, 'tcx>, ast::NodeId)) {
let def_id = self.tcx.hir.local_def_id(id);
- self.tcx.dep_graph.with_task(DepNode::CollectItemSig(def_id), || {
- self.tcx.hir.read(id);
- op();
- });
+ self.tcx.dep_graph.with_task(DepNode::CollectItemSig(def_id), self.tcx, id, op);
}
}
}
fn visit_item(&mut self, item: &'tcx hir::Item) {
- self.with_collect_item_sig(item.id, || convert_item(self.tcx, item));
+ self.with_collect_item_sig(item.id, convert_item);
intravisit::walk_item(self, item);
}
}
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
- self.with_collect_item_sig(trait_item.id, || {
- convert_trait_item(self.tcx, trait_item)
- });
+ self.with_collect_item_sig(trait_item.id, convert_trait_item);
intravisit::walk_trait_item(self, trait_item);
}
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
- self.with_collect_item_sig(impl_item.id, || {
- convert_impl_item(self.tcx, impl_item)
- });
+ self.with_collect_item_sig(impl_item.id, convert_impl_item);
intravisit::walk_impl_item(self, impl_item);
}
}
}
}
-fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, it: &hir::Item) {
+fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId) {
+ let it = tcx.hir.expect_item(item_id);
debug!("convert: item {} with id {}", it.name, it.id);
- let def_id = tcx.hir.local_def_id(it.id);
+ let def_id = tcx.hir.local_def_id(item_id);
match it.node {
// These don't define types.
hir::ItemExternCrate(_) | hir::ItemUse(..) | hir::ItemMod(_) => {
}
}
-fn convert_trait_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, trait_item: &hir::TraitItem) {
+fn convert_trait_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, trait_item_id: ast::NodeId) {
+ let trait_item = tcx.hir.expect_trait_item(trait_item_id);
let def_id = tcx.hir.local_def_id(trait_item.id);
tcx.item_generics(def_id);
tcx.item_predicates(def_id);
}
-fn convert_impl_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_item: &hir::ImplItem) {
- let def_id = tcx.hir.local_def_id(impl_item.id);
+fn convert_impl_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_item_id: ast::NodeId) {
+ let def_id = tcx.hir.local_def_id(impl_item_id);
tcx.item_generics(def_id);
tcx.item_type(def_id);
tcx.item_predicates(def_id);
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
/// rendering function with the necessary arguments for linking to a local path.
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
- print_all: bool, use_absolute: bool) -> fmt::Result {
+ print_all: bool, use_absolute: bool, is_not_debug: bool) -> fmt::Result {
let last = path.segments.last().unwrap();
let rel_root = match &*path.segments[0].name {
"self" => Some("./".to_string()),
} else {
root.push_str(&seg.name);
root.push_str("/");
- write!(w, "<a class=\"mod\"
- href=\"{}index.html\">{}</a>::",
- root,
- seg.name)?;
+ if is_not_debug {
+ write!(w, "<a class=\"mod\"
+ href=\"{}index.html\">{}</a>::",
+ root,
+ seg.name)?;
+ } else {
+ write!(w, "{}::", seg.name)?;
+ }
}
}
}
}
}
if w.alternate() {
- write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
+ if is_not_debug {
+ write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
+ } else {
+ write!(w, "{:?}{:?}", HRef::new(did, &last.name), last.params)?;
+ }
} else {
- let path = if use_absolute {
- match href(did) {
- Some((_, _, fqp)) => format!("{}::{}",
- fqp[..fqp.len()-1].join("::"),
- HRef::new(did, fqp.last().unwrap())),
- None => format!("{}", HRef::new(did, &last.name)),
- }
+ if is_not_debug {
+ let path = if use_absolute {
+ match href(did) {
+ Some((_, _, fqp)) => format!("{}::{}",
+ fqp[..fqp.len()-1].join("::"),
+ HRef::new(did, fqp.last().unwrap())),
+ None => format!("{}", HRef::new(did, &last.name)),
+ }
+ } else {
+ format!("{}", HRef::new(did, &last.name))
+ };
+ write!(w, "{}{}", path, last.params)?;
} else {
- format!("{}", HRef::new(did, &last.name))
- };
- write!(w, "{}{}", path, last.params)?;
+ let path = if use_absolute {
+ match href(did) {
+ Some((_, _, fqp)) => format!("{:?}::{:?}",
+ fqp[..fqp.len()-1].join("::"),
+ HRef::new(did, fqp.last().unwrap())),
+ None => format!("{:?}", HRef::new(did, &last.name)),
+ }
+ } else {
+ format!("{:?}", HRef::new(did, &last.name))
+ };
+ write!(w, "{}{:?}", path, last.params)?;
+ }
}
Ok(())
}
}
}
+impl<'a> fmt::Debug for HRef<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.text)
+ }
+}
+
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
is_not_debug: bool) -> fmt::Result {
match *t {
}
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
// Paths like T::Output and Self::Output should be rendered with all segments
- resolved_path(f, did, path, is_generic, use_absolute)?;
+ resolved_path(f, did, path, is_generic, use_absolute, is_not_debug)?;
tybounds(f, typarams)
}
clean::Infer => write!(f, "_"),
write!(f, "{}::", self_type)?;
}
let path = clean::Path::singleton(name.clone());
- resolved_path(f, did, &path, true, use_absolute)?;
+ resolved_path(f, did, &path, true, use_absolute, is_not_debug)?;
// FIXME: `typarams` are not rendered, and this seems bad?
drop(typarams);
impl fmt::Display for clean::ImportSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.did {
- Some(did) => resolved_path(f, did, &self.path, true, false),
+ Some(did) => resolved_path(f, did, &self.path, true, false, true),
_ => {
for (i, seg) in self.path.segments.iter().enumerate() {
if i > 0 {
use syntax::abi;
use syntax::ast;
use syntax::attr;
+use syntax::tokenstream::TokenStream;
use syntax_pos::Span;
use rustc::hir::map as hir_map;
}
let imported_from = self.cx.sess().cstore.original_crate_name(def_id.krate);
let def = match self.cx.sess().cstore.load_macro(def_id, self.cx.sess()) {
- LoadedMacro::MacroRules(macro_rules) => macro_rules,
+ LoadedMacro::MacroDef(macro_def) => macro_def,
// FIXME(jseyfried): document proc macro reexports
LoadedMacro::ProcMacro(..) => continue,
};
- // FIXME(jseyfried) merge with `self.visit_macro()`
- let tts = def.stream().trees().collect::<Vec<_>>();
- let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
+ let matchers = if let ast::ItemKind::MacroDef(ref tokens) = def.node {
+ let tts: Vec<_> = TokenStream::from(tokens.clone()).into_trees().collect();
+ tts.chunks(4).map(|arm| arm[0].span()).collect()
+ } else {
+ unreachable!()
+ };
om.macros.push(Macro {
def_id: def_id,
attrs: def.attrs.clone().into(),
pub module: Mod,
pub attrs: Vec<Attribute>,
pub span: Span,
- pub exported_macros: Vec<MacroDef>,
}
/// A spanned compile-time attribute list item.
Option<TraitRef>, // (optional) trait this impl implements
P<Ty>, // self
Vec<ImplItem>),
- /// A macro invocation (which includes macro definition).
+ /// A macro invocation.
///
/// E.g. `macro_rules! foo { .. }` or `foo!(..)`
Mac(Mac),
+
+ /// A macro definition.
+ MacroDef(ThinTokenStream),
}
impl ItemKind {
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::Mac(..) |
+ ItemKind::MacroDef(..) |
ItemKind::Impl(..) |
ItemKind::DefaultImpl(..) => "item"
}
}
}
-/// A macro definition, in this crate or imported from another.
-///
-/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
-pub struct MacroDef {
- pub ident: Ident,
- pub attrs: Vec<Attribute>,
- pub id: NodeId,
- pub span: Span,
- pub body: ThinTokenStream,
-}
-
-impl MacroDef {
- pub fn stream(&self) -> TokenStream {
- self.body.clone().into()
- }
-}
-
#[cfg(test)]
mod tests {
use serialize;
use codemap::{self, CodeMap, ExpnInfo, Spanned, respan};
use syntax_pos::{Span, ExpnId, NO_EXPANSION};
use errors::{DiagnosticBuilder, FatalError};
-use ext::expand::{self, Expansion};
+use ext::expand::{self, Expansion, Invocation};
use ext::hygiene::Mark;
use fold::{self, Folder};
use parse::{self, parser, DirectoryOwnership};
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]);
- fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
- fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
+ fn add_builtin(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
fn resolve_imports(&mut self);
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
- fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind,
- force: bool) -> Result<Rc<SyntaxExtension>, Determinacy>;
+ fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
+ -> Result<Option<Rc<SyntaxExtension>>, Determinacy>;
+ fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
+ -> Result<Rc<SyntaxExtension>, Determinacy>;
}
#[derive(Copy, Clone, Debug)]
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion, _derives: &[Mark]) {}
- fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
- fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
+ fn add_builtin(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
fn resolve_imports(&mut self) {}
fn find_legacy_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
+ fn resolve_invoc(&mut self, _invoc: &mut Invocation, _scope: Mark, _force: bool)
+ -> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
+ Err(Determinacy::Determined)
+ }
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _kind: MacroKind,
_force: bool) -> Result<Rc<SyntaxExtension>, Determinacy> {
Err(Determinacy::Determined)
pub struct Invocation {
pub kind: InvocationKind,
expansion_kind: ExpansionKind,
- expansion_data: ExpansionData,
+ pub expansion_data: ExpansionData,
}
pub enum InvocationKind {
let scope =
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
- let ext = match self.resolve_invoc(&mut invoc, scope, force) {
+ let ext = match self.cx.resolver.resolve_invoc(&mut invoc, scope, force) {
Ok(ext) => Some(ext),
Err(Determinacy::Determined) => None,
Err(Determinacy::Undetermined) => {
result
}
- fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
- -> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
- let (attr, traits, item) = match invoc.kind {
- InvocationKind::Bang { ref mac, .. } => {
- return self.cx.resolver.resolve_macro(scope, &mac.node.path,
- MacroKind::Bang, force).map(Some);
- }
- InvocationKind::Attr { attr: None, .. } => return Ok(None),
- InvocationKind::Derive { name, span, .. } => {
- let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
- return self.cx.resolver.resolve_macro(scope, &path,
- MacroKind::Derive, force).map(Some)
- }
- InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
- };
-
- let (attr_name, path) = {
- let attr = attr.as_ref().unwrap();
- (attr.name(), ast::Path::from_ident(attr.span, Ident::with_empty_ctxt(attr.name())))
- };
-
- let mut determined = true;
- match self.cx.resolver.resolve_macro(scope, &path, MacroKind::Attr, force) {
- Ok(ext) => return Ok(Some(ext)),
- Err(Determinacy::Undetermined) => determined = false,
- Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
- _ => {}
- }
-
- for &(name, span) in traits {
- let path = ast::Path::from_ident(span, Ident::with_empty_ctxt(name));
- match self.cx.resolver.resolve_macro(scope, &path, MacroKind::Derive, force) {
- Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs) = *ext {
- if inert_attrs.contains(&attr_name) {
- // FIXME(jseyfried) Avoid `mem::replace` here.
- let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
- .make_items().pop().unwrap();
- *item = mem::replace(item, Annotatable::Item(dummy_item))
- .map_attrs(|mut attrs| {
- let inert_attr = attr.take().unwrap();
- attr::mark_known(&inert_attr);
- if self.cx.ecfg.proc_macro_enabled() {
- *attr = find_attr_invoc(&mut attrs);
- }
- attrs.push(inert_attr);
- attrs
- });
- }
- return Err(Determinacy::Undetermined);
- },
- Err(Determinacy::Undetermined) => determined = false,
- Err(Determinacy::Determined) => {}
- }
- }
-
- Err(if determined { Determinacy::Determined } else { Determinacy::Undetermined })
- }
-
fn expand_invoc(&mut self, invoc: Invocation, ext: Rc<SyntaxExtension>) -> Expansion {
match invoc.kind {
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext),
let extname = path.segments.last().unwrap().identifier.name;
let ident = ident.unwrap_or(keywords::Invalid.ident());
- let marked_tts = mark_tts(mac.node.stream(), mark);
+ let marked_tts =
+ noop_fold_tts(mac.node.stream(), &mut Marker { mark: mark, expn_id: None });
let opt_expanded = match *ext {
NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
if ident.name != keywords::Invalid.name() {
}
}
-fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
+pub fn find_attr_invoc(attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
for i in 0 .. attrs.len() {
if !attr::is_known(&attrs[i]) && !is_builtin_attr(&attrs[i]) {
return Some(attrs.remove(i));
match item.node {
ast::ItemKind::Mac(..) => {
self.check_attributes(&item.attrs);
- let is_macro_def = if let ItemKind::Mac(ref mac) = item.node {
- mac.node.path.segments[0].identifier.name == "macro_rules"
- } else {
- unreachable!()
- };
-
- item.and_then(|mut item| match item.node {
- ItemKind::Mac(_) if is_macro_def => {
- item.id = Mark::fresh().as_placeholder_id();
- SmallVector::one(P(item))
- }
+ item.and_then(|item| match item.node {
ItemKind::Mac(mac) => {
self.collect(ExpansionKind::Items, InvocationKind::Bang {
mac: mac,
}
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
- noop_fold_item_kind(self.cfg.configure_item_kind(item), self)
+ match item {
+ ast::ItemKind::MacroDef(..) => item,
+ _ => noop_fold_item_kind(self.cfg.configure_item_kind(item), self),
+ }
}
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
span
}
}
-
-// apply a given mark to the given token trees. Used prior to expansion of a macro.
-pub fn mark_tts(tts: TokenStream, m: Mark) -> TokenStream {
- noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
-}
}
/// A mark is a unique id associated with a macro expansion.
-#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
+#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default, RustcEncodable, RustcDecodable)]
pub struct Mark(u32);
impl Mark {
})
})
}
-
- /// If `ident` is macro expanded, return the source ident from the macro definition
- /// and the mark of the expansion that created the macro definition.
- pub fn source(self) -> (Self /* source context */, Mark /* source macro */) {
- let macro_def_ctxt = self.data().prev_ctxt.data();
- (macro_def_ctxt.prev_ctxt, macro_def_ctxt.outer_mark)
- }
}
impl fmt::Debug for SyntaxContext {
use util::small_vector::SmallVector;
use std::collections::HashMap;
-use std::mem;
pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
fn mac_placeholder() -> ast::Mac {
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
noop_fold_block(block, self).map(|mut block| {
- let mut macros = Vec::new();
let mut remaining_stmts = block.stmts.len();
block.stmts = block.stmts.move_flat_map(|mut stmt| {
remaining_stmts -= 1;
- // `macro_rules!` macro definition
- if let ast::StmtKind::Item(ref item) = stmt.node {
- if let ast::ItemKind::Mac(_) = item.node {
- macros.push(Mark::from_placeholder_id(item.id));
- return None;
- }
- }
-
match stmt.node {
// Avoid wasting a node id on a trailing expression statement,
// which shares a HIR node with the expression itself.
_ => {}
}
- if self.monotonic && !macros.is_empty() {
- let macros = mem::replace(&mut macros, Vec::new());
- self.cx.resolver.add_expansions_at_stmt(stmt.id, macros);
- }
-
Some(stmt)
});
// Holy self-referential!
/// Converts a `macro_rules!` invocation into a syntax extension.
-pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension {
+pub fn compile(sess: &ParseSess, def: &ast::Item) -> SyntaxExtension {
let lhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("lhs"));
let rhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("rhs"));
];
// Parse the macro_rules! invocation
- let argument_map = match parse(sess, def.body.clone().into(), &argument_gram, None) {
+ let body = match def.node {
+ ast::ItemKind::MacroDef(ref body) => body.clone().into(),
+ _ => unreachable!(),
+ };
+ let argument_map = match parse(sess, body, &argument_gram, None) {
Success(m) => m,
Failure(sp, tok) => {
let s = parse_failure_msg(tok);
items.move_flat_map(|item| folder.fold_trait_item(item)),
),
ItemKind::Mac(m) => ItemKind::Mac(folder.fold_mac(m)),
+ ItemKind::MacroDef(tts) => ItemKind::MacroDef(folder.fold_tts(tts.into()).into()),
}
}
}
}
-pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, mut exported_macros, span}: Crate,
+pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
folder: &mut T) -> Crate {
let mut items = folder.fold_item(P(ast::Item {
ident: keywords::Invalid.ident(),
}, vec![], span)
};
- for def in &mut exported_macros {
- def.id = folder.new_id(def.id);
- }
-
Crate {
module: module,
attrs: attrs,
- exported_macros: exported_macros,
span: span,
}
}
matches_codepattern,
"matches_codepattern",
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
- "zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
+ "macro_rules! zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
}
}
self.expected_tokens.clear();
}
- pub fn look_ahead<R, F>(&mut self, dist: usize, f: F) -> R where
+ pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
F: FnOnce(&token::Token) -> R,
{
if dist == 0 {
})
}
- fn is_union_item(&mut self) -> bool {
+ fn is_union_item(&self) -> bool {
self.token.is_keyword(keywords::Union) &&
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
}
+ fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility)
+ -> PResult<'a, Option<P<Item>>> {
+ let lo = self.span.lo;
+ match self.token {
+ token::Ident(ident) if ident.name == "macro_rules" => {
+ if self.look_ahead(1, |t| *t == token::Not) {
+ let prev_span = self.prev_span;
+ self.complain_if_pub_macro(vis, prev_span);
+ self.bump();
+ self.bump();
+ }
+ }
+ _ => return Ok(None),
+ };
+
+ let id = self.parse_ident()?;
+ let (delim, tts) = self.expect_delimited_token_tree()?;
+ if delim != token::Brace {
+ if !self.eat(&token::Semi) {
+ let msg = "macros that expand to items must either be surrounded with braces \
+ or followed by a semicolon";
+ self.span_err(self.prev_span, msg);
+ }
+ }
+
+ let hi = self.prev_span.hi;
+ let kind = ItemKind::MacroDef(tts);
+ Ok(Some(self.mk_item(lo, hi, id, kind, Visibility::Inherited, attrs.to_owned())))
+ }
+
fn parse_stmt_without_recovery(&mut self,
macro_legacy_warnings: bool)
-> PResult<'a, Option<Stmt>> {
node: StmtKind::Local(self.parse_local(attrs.into())?),
span: mk_sp(lo, self.prev_span.hi),
}
+ } else if let Some(macro_def) = self.eat_macro_def(&attrs, &Visibility::Inherited)? {
+ Stmt {
+ id: ast::DUMMY_NODE_ID,
+ node: StmtKind::Item(macro_def),
+ span: mk_sp(lo, self.prev_span.hi),
+ }
// Starts like a simple path, but not a union item.
} else if self.token.is_path_start() &&
!self.token.is_qpath_start() &&
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
+ if let Some(macro_def) = self.eat_macro_def(&attrs, &visibility)? {
+ return Ok(Some(macro_def));
+ }
+
self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
}
attrs: self.parse_inner_attributes()?,
module: self.parse_mod_items(&token::Eof, lo)?,
span: mk_sp(lo, self.span.lo),
- exported_macros: Vec::new(),
})
}
self.bclose(item.span)?;
}
ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => {
- self.print_visibility(&item.vis)?;
self.print_path(&node.path, false, 0, false)?;
word(&mut self.s, "! ")?;
self.print_ident(item.ident)?;
word(&mut self.s, ";")?;
self.end()?;
}
+ ast::ItemKind::MacroDef(ref tts) => {
+ word(&mut self.s, "macro_rules! ")?;
+ self.print_ident(item.ident)?;
+ self.cbox(INDENT_UNIT)?;
+ self.popen()?;
+ self.print_tts(tts.clone().into())?;
+ self.pclose()?;
+ word(&mut self.s, ";")?;
+ self.end()?;
+ }
}
self.ann.post(self, NodeItem(item))
}
fn visit_attribute(&mut self, _attr: &Attribute) {
self.count += 1;
}
- fn visit_macro_def(&mut self, macro_def: &MacroDef) {
- self.count += 1;
- walk_macro_def(self, macro_def)
- }
-
}
walk_assoc_type_binding(self, type_binding)
}
fn visit_attribute(&mut self, _attr: &'ast Attribute) {}
- fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
- walk_macro_def(self, macro_def)
- }
fn visit_vis(&mut self, vis: &'ast Visibility) {
walk_vis(self, vis)
}
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
walk_list!(visitor, visit_attribute, &krate.attrs);
- walk_list!(visitor, visit_macro_def, &krate.exported_macros);
-}
-
-pub fn walk_macro_def<'a, V: Visitor<'a>>(visitor: &mut V, macro_def: &'a MacroDef) {
- visitor.visit_ident(macro_def.span, macro_def.ident);
- walk_list!(visitor, visit_attribute, ¯o_def.attrs);
}
pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
walk_list!(visitor, visit_trait_item, methods);
}
ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
+ ItemKind::MacroDef(..) => {},
}
walk_list!(visitor, visit_attribute, &item.attrs);
}
pub fn register_builtin_derives(resolver: &mut Resolver) {
$(
- resolver.add_ext(
+ resolver.add_builtin(
ast::Ident::with_empty_ctxt(Symbol::intern($name)),
Rc::new(SyntaxExtension::BuiltinDerive($func))
);
deriving::register_builtin_derives(resolver);
let mut register = |name, ext| {
- resolver.add_ext(ast::Ident::with_empty_ctxt(name), Rc::new(ext));
+ resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Rc::new(ext));
};
macro_rules! register {
krate.module.items.push(mk_registrar(&mut cx, &derives, &attr_macros, &bang_macros));
- if krate.exported_macros.len() > 0 {
- handler.err("cannot export macro_rules! macros from a `proc-macro` \
- crate type currently");
- }
-
- return krate
+ krate
}
fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
impl<'a> Visitor<'a> for CollectProcMacros<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
+ if let ast::ItemKind::MacroDef(..) = item.node {
+ if self.is_proc_macro_crate &&
+ item.attrs.iter().any(|attr| attr.name() == "macro_export") {
+ let msg =
+ "cannot export macro_rules! macros from a `proc-macro` crate type currently";
+ self.handler.span_err(item.span, msg);
+ }
+ }
+
// First up, make sure we're checking a bare function. If we're not then
// we're just not interested in this item.
//
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn prove_static<T: 'static + ?Sized>(_: &'static T) {}
+
+fn lifetime_transmute_slice<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
+ let mut out = [x];
+ //~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
+ {
+ let slice: &mut [_] = &mut out;
+ slice[0] = y;
+ }
+ out[0]
+}
+
+struct Struct<T, U: ?Sized> {
+ head: T,
+ _tail: U
+}
+
+fn lifetime_transmute_struct<'a, T: ?Sized>(x: &'a T, y: &T) -> &'a T {
+ let mut out = Struct { head: x, _tail: [()] };
+ //~^ ERROR cannot infer an appropriate lifetime due to conflicting requirements
+ {
+ let dst: &mut Struct<_, [()]> = &mut out;
+ dst.head = y;
+ }
+ out.head
+}
+
+fn main() {
+ prove_static(lifetime_transmute_slice("", &String::from("foo")));
+ prove_static(lifetime_transmute_struct("", &String::from("bar")));
+}
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn save_ref<'a>(refr: &'a i32, to: &mut [&'a i32]) {
+ for val in &mut *to {
+ *val = refr;
+ }
+}
+
+fn main() {
+ let ref init = 0i32;
+ let ref mut refr = 1i32;
+
+ let mut out = [init];
+
+ save_ref(&*refr, &mut out);
+
+ // This shouldn't be allowed as `refr` is borrowed
+ *refr = 3; //~ ERROR cannot assign to `*refr` because it is borrowed
+
+ // Prints 3?!
+ println!("{:?}", out[0]);
+}
// which fails to type check.
ss
- //~^ ERROR lifetime bound not satisfied
+ //~^ ERROR cannot infer
//~| ERROR cannot infer
}
// `Box<SomeTrait>` defaults to a `'static` bound, so this return
// is illegal.
- ss.r //~ ERROR lifetime bound not satisfied
+ ss.r //~ ERROR cannot infer an appropriate lifetime
}
fn store(ss: &mut SomeStruct, b: Box<SomeTrait>) {
fn store1<'b>(ss: &mut SomeStruct, b: Box<SomeTrait+'b>) {
// Here we override the lifetimes explicitly, and so naturally we get an error.
- ss.r = b; //~ ERROR lifetime bound not satisfied
+ ss.r = b; //~ ERROR cannot infer an appropriate lifetime
}
fn main() {
fn make_object_bad<'a,'b,'c,A:SomeTrait+'a+'b>(v: A) -> Box<SomeTrait+'c> {
// A outlives 'a AND 'b...but not 'c.
- box v as Box<SomeTrait+'a> //~ ERROR lifetime bound not satisfied
+ box v as Box<SomeTrait+'a> //~ ERROR cannot infer an appropriate lifetime
}
fn main() {
fn static_proc(x: &isize) -> Box<FnMut()->(isize) + 'static> {
// This is illegal, because the region bound on `proc` is 'static.
- Box::new(move|| { *x }) //~ ERROR does not fulfill the required lifetime
+ Box::new(move|| { *x }) //~ ERROR cannot infer an appropriate lifetime
}
fn main() { }
fn foo3<'a,'b>(x: &'a mut Dummy) -> &'b mut Dummy {
// Without knowing 'a:'b, we can't coerce
- x //~ ERROR lifetime bound not satisfied
- //~^ ERROR cannot infer
+ x //~ ERROR cannot infer an appropriate lifetime
+ //~^ ERROR cannot infer an appropriate lifetime
}
struct Wrapper<T>(T);
-> Box<Get<&'min i32>>
where 'max : 'min
{
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn get_max_from_min<'min, 'max, G>(v: Box<Get<&'min i32>>)
where 'max : 'min
{
// Previously OK:
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn main() { }
where 'max : 'min
{
// Previously OK, now an error as traits are invariant.
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn get_max_from_min<'min, 'max, G>(v: Box<Get<&'min i32>>)
-> Box<Get<&'max i32>>
where 'max : 'min
{
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn main() { }
-> Box<Get<&'min i32>>
where 'max : 'min
{
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn get_max_from_min<'min, 'max, G>(v: Box<Get<&'min i32>>)
-> Box<Get<&'max i32>>
where 'max : 'min
{
- v //~ ERROR mismatched types
+ v //~ ERROR cannot infer an appropriate lifetime
}
fn main() { }
// === GDB TESTS ===================================================================================
-// gdb-command:print 'c_style_enum::SINGLE_VARIANT'
+// gdbg-command:print 'c_style_enum::SINGLE_VARIANT'
+// gdbr-command:print c_style_enum::SINGLE_VARIANT
// gdbg-check:$1 = TheOnlyVariant
// gdbr-check:$1 = c_style_enum::SingleVariant::TheOnlyVariant
-// gdb-command:print 'c_style_enum::AUTO_ONE'
+// gdbg-command:print 'c_style_enum::AUTO_ONE'
+// gdbr-command:print c_style_enum::AUTO_ONE
// gdbg-check:$2 = One
// gdbr-check:$2 = c_style_enum::AutoDiscriminant::One
-// gdb-command:print 'c_style_enum::AUTO_TWO'
+// gdbg-command:print 'c_style_enum::AUTO_TWO'
+// gdbr-command:print c_style_enum::AUTO_TWO
// gdbg-check:$3 = One
// gdbr-check:$3 = c_style_enum::AutoDiscriminant::One
-// gdb-command:print 'c_style_enum::AUTO_THREE'
+// gdbg-command:print 'c_style_enum::AUTO_THREE'
+// gdbr-command:print c_style_enum::AUTO_THREE
// gdbg-check:$4 = One
// gdbr-check:$4 = c_style_enum::AutoDiscriminant::One
-// gdb-command:print 'c_style_enum::MANUAL_ONE'
+// gdbg-command:print 'c_style_enum::MANUAL_ONE'
+// gdbr-command:print c_style_enum::MANUAL_ONE
// gdbg-check:$5 = OneHundred
// gdbr-check:$5 = c_style_enum::ManualDiscriminant::OneHundred
-// gdb-command:print 'c_style_enum::MANUAL_TWO'
+// gdbg-command:print 'c_style_enum::MANUAL_TWO'
+// gdbr-command:print c_style_enum::MANUAL_TWO
// gdbg-check:$6 = OneHundred
// gdbr-check:$6 = c_style_enum::ManualDiscriminant::OneHundred
-// gdb-command:print 'c_style_enum::MANUAL_THREE'
+// gdbg-command:print 'c_style_enum::MANUAL_THREE'
+// gdbr-command:print c_style_enum::MANUAL_THREE
// gdbg-check:$7 = OneHundred
// gdbr-check:$7 = c_style_enum::ManualDiscriminant::OneHundred
// Make sure functions have proper names
// gdb-command:info functions
-// gdb-check:[...]void[...]main([...]);
-// gdb-check:[...]void[...]some_function([...]);
-// gdb-check:[...]void[...]some_other_function([...]);
-// gdb-check:[...]void[...]zzz([...]);
+// gdbg-check:[...]void[...]main([...]);
+// gdbr-check:fn limited_debuginfo::main();
+// gdbg-check:[...]void[...]some_function([...]);
+// gdbr-check:fn limited_debuginfo::some_function();
+// gdbg-check:[...]void[...]some_other_function([...]);
+// gdbr-check:fn limited_debuginfo::some_other_function();
+// gdbg-check:[...]void[...]zzz([...]);
+// gdbr-check:fn limited_debuginfo::zzz();
// gdb-command:run
// === GDB TESTS ===================================================================================
-// there's no frame yet for gdb to reliably detect the language, set it explicitly
-// gdbr-command:set language rust
-
// gdbg-command:print 'simple_struct::NO_PADDING_16'
// gdbr-command:print simple_struct::NO_PADDING_16
// gdbg-check:$1 = {x = 1000, y = -1001}
// === GDB TESTS ===================================================================================
-// there's no frame yet for gdb to reliably detect the language, set it explicitly
-// gdbr-command:set language rust
-
// gdbg-command:print/d 'simple_tuple::NO_PADDING_8'
// gdbr-command:print simple_tuple::NO_PADDING_8
// gdbg-check:$1 = {__0 = -50, __1 = 50}
// }
//
// bb2: {
-// StorageLive(_6);
// _0 = ();
// StorageDead(_4);
// StorageDead(_1);
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use std::fmt;
+
#[repr(packed)]
-#[derive(Copy, Clone, PartialEq, Debug)]
+#[derive(Copy, Clone)]
struct Foo {
a: i8,
b: i16,
c: i8
}
+impl PartialEq for Foo {
+ fn eq(&self, other: &Foo) -> bool {
+ self.a == other.a && self.b == other.b && self.c == other.c
+ }
+}
+
+impl fmt::Debug for Foo {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let a = self.a;
+ let b = self.b;
+ let c = self.c;
+
+ f.debug_struct("Foo")
+ .field("a", &a)
+ .field("b", &b)
+ .field("c", &c)
+ .finish()
+ }
+}
+
#[link(name = "test", kind = "static")]
extern {
fn foo(f: Foo) -> Foo;
#![feature(no_core)]
#![no_core]
+macro_rules! foo /* 60#0 */(( $ x : ident ) => { y + $ x });
fn bar /* 62#0 */() { let x /* 59#2 */ = 1; y /* 61#4 */ + x /* 59#5 */ }
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// pretty-expanded FIXME #23616
+
+use std::rc::Rc;
+
+fn lub_short<'a, T>(_: &[&'a T], _: &[&'a T]) {}
+
+// The two arguments are a subtype of their LUB, after coercion.
+fn long_and_short<'a, T>(xs: &[&'static T; 1], ys: &[&'a T; 1]) {
+ lub_short(xs, ys);
+}
+
+// The argument coerces to a subtype of the return type.
+fn long_to_short<'a, 'b, T>(xs: &'b [&'static T; 1]) -> &'b [&'a T] {
+ xs
+}
+
+// Rc<T> is covariant over T just like &T.
+fn long_to_short_rc<'a, T>(xs: Rc<[&'static T; 1]>) -> Rc<[&'a T]> {
+ xs
+}
+
+// LUB-coercion (if-else/match/array) coerces `xs: &'b [&'static T: N]`
+// to a subtype of the LUB of `xs` and `ys` (i.e. `&'b [&'a T]`),
+// regardless of the order they appear (in if-else/match/array).
+fn long_and_short_lub1<'a, 'b, T>(xs: &'b [&'static T; 1], ys: &'b [&'a T]) {
+ let _order1 = [xs, ys];
+ let _order2 = [ys, xs];
+}
+
+// LUB-coercion should also have the exact same effect when `&'b [&'a T; N]`
+// needs to be coerced, i.e. the resulting type is not &'b [&'static T], but
+// rather the `&'b [&'a T]` LUB.
+fn long_and_short_lub2<'a, 'b, T>(xs: &'b [&'static T], ys: &'b [&'a T; 1]) {
+ let _order1 = [xs, ys];
+ let _order2 = [ys, xs];
+}
+
+fn main() {}
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use std::fmt;
use std::mem;
#[repr(packed)]
-#[derive(Copy, Clone, PartialEq, Debug)]
+#[derive(Copy, Clone)]
struct Foo {
bar: u8,
baz: u64
}
+impl PartialEq for Foo {
+ fn eq(&self, other: &Foo) -> bool {
+ self.bar == other.bar && self.baz == other.baz
+ }
+}
+
+impl fmt::Debug for Foo {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let bar = self.bar;
+ let baz = self.baz;
+
+ f.debug_struct("Foo")
+ .field("bar", &bar)
+ .field("baz", &baz)
+ .finish()
+ }
+}
+
pub fn main() {
let foos = [Foo { bar: 1, baz: 2 }; 10];
+++ /dev/null
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-fn main() {
- let (x,) = (vec![],);
-}
+++ /dev/null
-error[E0282]: type annotations needed
- --> $DIR/issue-38812-2.rs:12:17
- |
-12 | let (x,) = (vec![],);
- | ---- ^^^^^^ cannot infer type for `T`
- | |
- | consider giving a type to pattern
- |
- = note: this error originates in a macro outside of the current crate
-
-error: aborting due to previous error
-
+++ /dev/null
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-fn main() {
- let x = vec![];
-}
+++ /dev/null
-error[E0282]: type annotations needed
- --> $DIR/issue-38812.rs:12:13
- |
-12 | let x = vec![];
- | - ^^^^^^ cannot infer type for `T`
- | |
- | consider giving `x` a type
- |
- = note: this error originates in a macro outside of the current crate
-
-error: aborting due to previous error
-
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+ let (x,) = (vec![],);
+}
--- /dev/null
+error[E0282]: type annotations needed
+ --> $DIR/issue-38812-2.rs:12:17
+ |
+12 | let (x,) = (vec![],);
+ | ---- ^^^^^^ cannot infer type for `T`
+ | |
+ | consider giving a type to pattern
+ |
+ = note: this error originates in a macro outside of the current crate
+
+error: aborting due to previous error
+
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+ let x = vec![];
+}
--- /dev/null
+error[E0282]: type annotations needed
+ --> $DIR/issue-38812.rs:12:13
+ |
+12 | let x = vec![];
+ | - ^^^^^^ cannot infer type for `T`
+ | |
+ | consider giving `x` a type
+ |
+ = note: this error originates in a macro outside of the current crate
+
+error: aborting due to previous error
+
--- /dev/null
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Foo: Sized {
+ fn foo(self);
+}
+
+fn foo<'a,'b,T>(x: &'a T, y: &'b T)
+ where &'a T : Foo,
+ &'b T : Foo
+{
+ x.foo();
+ y.foo();
+}
+
+fn main() { }
--- /dev/null
+error[E0282]: type annotations needed
+ --> $DIR/issue-40294.rs:15:1
+ |
+15 | fn foo<'a,'b,T>(x: &'a T, y: &'b T)
+ | _^ starting here...
+16 | | where &'a T : Foo,
+17 | | &'b T : Foo
+18 | | {
+19 | | x.foo();
+20 | | y.foo();
+21 | | }
+ | |_^ ...ending here: cannot infer type for `&'a T`
+
+error: aborting due to previous error
+