ecc6c39e87
This commit is an implementation of [RFC 1681] which adds support to the compiler for first-class user-define custom `#[derive]` modes with a far more stable API than plugins have today. [RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md The main features added by this commit are: * A new `rustc-macro` crate-type. This crate type represents one which will provide custom `derive` implementations and perhaps eventually flower into the implementation of macros 2.0 as well. * A new `rustc_macro` crate in the standard distribution. This crate will provide the runtime interface between macro crates and the compiler. The API here is particularly conservative right now but has quite a bit of room to expand into any manner of APIs required by macro authors. * The ability to load new derive modes through the `#[macro_use]` annotations on other crates. All support added here is gated behind the `rustc_macro` feature gate, both for the library support (the `rustc_macro` crate) as well as the language features. There are a few minor differences from the implementation outlined in the RFC, such as the `rustc_macro` crate being available as a dylib and all symbols are `dlsym`'d directly instead of having a shim compiled. These should only affect the implementation, however, not the public interface. This commit also ended up touching a lot of code related to `#[derive]`, making a few notable changes: * Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't sure how to keep this behavior and *not* expose it to custom derive. * Derive attributes no longer have access to unstable features by default, they have to opt in on a granular level. * The `derive(Copy,Clone)` optimization is now done through another "obscure attribute" which is just intended to ferry along in the compiler that such an optimization is possible. The `derive(PartialEq,Eq)` optimization was also updated to do something similar. --- One part of this PR which needs to be improved before stabilizing are the errors and exact interfaces here. The error messages are relatively poor quality and there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]` not working by default. The custom attributes added by the compiler end up becoming unstable again when going through a custom impl. Hopefully though this is enough to start allowing experimentation on crates.io! syntax-[breaking-change]
104 lines
3.0 KiB
Rust
104 lines
3.0 KiB
Rust
// Copyright 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.
|
|
|
|
// Regression test for #20797.
|
|
|
|
#![feature(question_mark)]
|
|
|
|
use std::default::Default;
|
|
use std::io;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
pub trait PathExtensions {
|
|
fn is_dir(&self) -> bool { false }
|
|
}
|
|
|
|
impl PathExtensions for PathBuf {}
|
|
|
|
/// A strategy for acquiring more subpaths to walk.
|
|
pub trait Strategy {
|
|
type P: PathExtensions;
|
|
/// Get additional subpaths from a given path.
|
|
fn get_more(&self, item: &Self::P) -> io::Result<Vec<Self::P>>;
|
|
/// Determine whether a path should be walked further.
|
|
/// This is run against each item from `get_more()`.
|
|
fn prune(&self, p: &Self::P) -> bool;
|
|
}
|
|
|
|
/// The basic fully-recursive strategy. Nothing is pruned.
|
|
#[derive(Copy, Clone, Default)]
|
|
pub struct Recursive;
|
|
|
|
impl Strategy for Recursive {
|
|
type P = PathBuf;
|
|
fn get_more(&self, p: &PathBuf) -> io::Result<Vec<PathBuf>> {
|
|
Ok(fs::read_dir(p).unwrap().map(|s| s.unwrap().path()).collect())
|
|
}
|
|
|
|
fn prune(&self, _: &PathBuf) -> bool { false }
|
|
}
|
|
|
|
/// A directory walker of `P` using strategy `S`.
|
|
pub struct Subpaths<S: Strategy> {
|
|
stack: Vec<S::P>,
|
|
strategy: S,
|
|
}
|
|
|
|
impl<S: Strategy> Subpaths<S> {
|
|
/// Create a directory walker with a root path and strategy.
|
|
pub fn new(p: &S::P, strategy: S) -> io::Result<Subpaths<S>> {
|
|
let stack = strategy.get_more(p)?;
|
|
Ok(Subpaths { stack: stack, strategy: strategy })
|
|
}
|
|
}
|
|
|
|
impl<S: Default + Strategy> Subpaths<S> {
|
|
/// Create a directory walker with a root path and a default strategy.
|
|
pub fn walk(p: &S::P) -> io::Result<Subpaths<S>> {
|
|
Subpaths::new(p, Default::default())
|
|
}
|
|
}
|
|
|
|
impl<S: Default + Strategy> Default for Subpaths<S> {
|
|
fn default() -> Subpaths<S> {
|
|
Subpaths { stack: Vec::new(), strategy: Default::default() }
|
|
}
|
|
}
|
|
|
|
impl<S: Strategy> Iterator for Subpaths<S> {
|
|
type Item = S::P;
|
|
fn next (&mut self) -> Option<S::P> {
|
|
let mut opt_path = self.stack.pop();
|
|
while opt_path.is_some() && self.strategy.prune(opt_path.as_ref().unwrap()) {
|
|
opt_path = self.stack.pop();
|
|
}
|
|
match opt_path {
|
|
Some(path) => {
|
|
if path.is_dir() {
|
|
let result = self.strategy.get_more(&path);
|
|
match result {
|
|
Ok(dirs) => { self.stack.extend(dirs); },
|
|
Err(..) => { }
|
|
}
|
|
}
|
|
Some(path)
|
|
}
|
|
None => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn _foo() {
|
|
let _walker: Subpaths<Recursive> = Subpaths::walk(&PathBuf::from("/home")).unwrap();
|
|
}
|
|
|
|
fn main() {}
|