rustc: Add a path attribute for crate directives
The path information was an optional "filename" component of crate directive AST. It is now replaced by an attribute with metadata named "path". With this commit, a directive mod foo = "foo.rs"; should be written as: #[path = "foo.rs"] mod foo; Closes issue #906.
This commit is contained in:
parent
547ec241bd
commit
3e303af86b
|
@ -1030,9 +1030,11 @@ An example of a crate:
|
|||
use std (ver = "1.0");
|
||||
|
||||
// Define some modules.
|
||||
mod foo = "foo.rs";
|
||||
#[path = "foo.rs"]
|
||||
mod foo;
|
||||
mod bar @{
|
||||
mod quux = "quux.rs";
|
||||
#[path = "quux.rs"]
|
||||
mod quux;
|
||||
@}
|
||||
@end example
|
||||
|
||||
|
|
|
@ -40,7 +40,8 @@ mod middle {
|
|||
mod tstate {
|
||||
mod ck;
|
||||
mod annotate;
|
||||
mod aux = "auxiliary.rs";
|
||||
#[path = "auxiliary.rs"]
|
||||
mod aux;
|
||||
mod bitvectors;
|
||||
mod collect_locals;
|
||||
mod pre_post_conditions;
|
||||
|
|
|
@ -58,8 +58,8 @@ type crate_ =
|
|||
config: crate_cfg};
|
||||
|
||||
tag crate_directive_ {
|
||||
cdir_src_mod(ident, option::t<filename>, [attribute]);
|
||||
cdir_dir_mod(ident, option::t<filename>, [@crate_directive], [attribute]);
|
||||
cdir_src_mod(ident, [attribute]);
|
||||
cdir_dir_mod(ident, [@crate_directive], [attribute]);
|
||||
cdir_view_item(@view_item);
|
||||
cdir_syntax(path);
|
||||
}
|
||||
|
|
|
@ -159,11 +159,11 @@ fn noop_fold_crate(c: crate_, fld: ast_fold) -> crate_ {
|
|||
fn noop_fold_crate_directive(cd: crate_directive_, fld: ast_fold) ->
|
||||
crate_directive_ {
|
||||
ret alt cd {
|
||||
cdir_src_mod(id, fname, attrs) {
|
||||
cdir_src_mod(fld.fold_ident(id), fname, attrs)
|
||||
cdir_src_mod(id, attrs) {
|
||||
cdir_src_mod(fld.fold_ident(id), attrs)
|
||||
}
|
||||
cdir_dir_mod(id, fname, cds, attrs) {
|
||||
cdir_dir_mod(fld.fold_ident(id), fname,
|
||||
cdir_dir_mod(id, cds, attrs) {
|
||||
cdir_dir_mod(fld.fold_ident(id),
|
||||
vec::map(fld.fold_crate_directive, cds), attrs)
|
||||
}
|
||||
cdir_view_item(vi) { cdir_view_item(fld.fold_view_item(vi)) }
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
|
||||
import front::attr;
|
||||
import std::{option, result, io, fs};
|
||||
import std::option::{some, none};
|
||||
import syntax::ast;
|
||||
|
@ -86,13 +87,21 @@ fn parse_companion_mod(cx: ctx, prefix: str, suffix: option::t<str>)
|
|||
}
|
||||
}
|
||||
|
||||
fn cdir_path_opt(id: str, attrs: [ast::attribute]) -> str {
|
||||
alt attr::get_meta_item_value_str_by_name(attrs, "path") {
|
||||
some(d) {
|
||||
ret d;
|
||||
}
|
||||
none. { ret id; }
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
|
||||
&view_items: [@ast::view_item],
|
||||
&items: [@ast::item]) {
|
||||
alt cdir.node {
|
||||
ast::cdir_src_mod(id, file_opt, attrs) {
|
||||
let file_path = id + ".rs";
|
||||
alt file_opt { some(f) { file_path = f; } none. { } }
|
||||
ast::cdir_src_mod(id, attrs) {
|
||||
let file_path = cdir_path_opt(id + ".rs", attrs);
|
||||
let full_path =
|
||||
if std::fs::path_is_absolute(file_path) {
|
||||
file_path
|
||||
|
@ -113,9 +122,8 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
|
|||
cx.byte_pos = p0.get_byte_pos();
|
||||
items += [i];
|
||||
}
|
||||
ast::cdir_dir_mod(id, dir_opt, cdirs, attrs) {
|
||||
let path = id;
|
||||
alt dir_opt { some(d) { path = d; } none. { } }
|
||||
ast::cdir_dir_mod(id, cdirs, attrs) {
|
||||
let path = cdir_path_opt(id, attrs);
|
||||
let full_path =
|
||||
if std::fs::path_is_absolute(path) {
|
||||
path
|
||||
|
|
|
@ -2475,19 +2475,12 @@ fn parse_crate_directive(p: parser, first_outer_attr: [ast::attribute]) ->
|
|||
if expect_mod || is_word(p, "mod") {
|
||||
expect_word(p, "mod");
|
||||
let id = parse_ident(p);
|
||||
let file_opt =
|
||||
alt p.peek() {
|
||||
token::EQ. { p.bump(); some(parse_str(p)) }
|
||||
_ {
|
||||
attr::get_meta_item_value_str_by_name(outer_attrs, "path")
|
||||
}
|
||||
};
|
||||
alt p.peek() {
|
||||
// mod x = "foo.rs";
|
||||
token::SEMI. {
|
||||
let hi = p.get_hi_pos();
|
||||
p.bump();
|
||||
ret spanned(lo, hi, ast::cdir_src_mod(id, file_opt, outer_attrs));
|
||||
ret spanned(lo, hi, ast::cdir_src_mod(id, outer_attrs));
|
||||
}
|
||||
// mod x = "foo_dir" { ...directives... }
|
||||
token::LBRACE. {
|
||||
|
@ -2500,7 +2493,7 @@ fn parse_crate_directive(p: parser, first_outer_attr: [ast::attribute]) ->
|
|||
let hi = p.get_hi_pos();
|
||||
expect(p, token::RBRACE);
|
||||
ret spanned(lo, hi,
|
||||
ast::cdir_dir_mod(id, file_opt, cdirs, mod_attrs));
|
||||
ast::cdir_dir_mod(id, cdirs, mod_attrs));
|
||||
}
|
||||
t { unexpected(p, t); }
|
||||
}
|
||||
|
|
|
@ -56,8 +56,8 @@ fn visit_crate<E>(c: crate, e: E, v: vt<E>) {
|
|||
|
||||
fn visit_crate_directive<E>(cd: @crate_directive, e: E, v: vt<E>) {
|
||||
alt cd.node {
|
||||
cdir_src_mod(_, _, _) { }
|
||||
cdir_dir_mod(_, _, cdirs, _) {
|
||||
cdir_src_mod(_, _) { }
|
||||
cdir_dir_mod(_, cdirs, _) {
|
||||
for cdir: @crate_directive in cdirs {
|
||||
visit_crate_directive(cdir, e, v);
|
||||
}
|
||||
|
|
|
@ -40,7 +40,8 @@ mod comm;
|
|||
mod fs;
|
||||
mod io;
|
||||
mod net;
|
||||
mod run = "run_program.rs";
|
||||
#[path = "run_program.rs"]
|
||||
mod run;
|
||||
mod sys;
|
||||
mod task;
|
||||
|
||||
|
@ -100,19 +101,25 @@ mod test;
|
|||
mod generic_os;
|
||||
|
||||
#[cfg(target_os = "win32")]
|
||||
mod os = "win32_os.rs";
|
||||
#[path = "win32_os.rs"]
|
||||
mod os;
|
||||
#[cfg(target_os = "win32")]
|
||||
mod os_fs = "win32_fs.rs";
|
||||
#[path = "win32_fs.rs"]
|
||||
mod os_fs;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
mod os = "macos_os.rs";
|
||||
#[path = "macos_os.rs"]
|
||||
mod os;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod os_fs = "posix_fs.rs";
|
||||
#[path = "posix_fs.rs"]
|
||||
mod os_fs;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod os = "linux_os.rs";
|
||||
#[path = "linux_os.rs"]
|
||||
mod os;
|
||||
#[cfg(target_os = "linux")]
|
||||
mod os_fs = "posix_fs.rs";
|
||||
#[path = "posix_fs.rs"]
|
||||
mod os_fs;
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust;
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
// error-pattern: expected string literal
|
||||
// Issue #1028
|
||||
mod ncurses = x;
|
|
@ -1,10 +1,12 @@
|
|||
// Test that crates and directory modules can contain code
|
||||
|
||||
mod a = "companionmod-src" {
|
||||
#[path = "companionmod-src"]
|
||||
mod a {
|
||||
mod b {
|
||||
mod x;
|
||||
}
|
||||
mod c = "d" {
|
||||
#[path = "d"]
|
||||
mod c {
|
||||
mod x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
#[vers = "1.0"];
|
||||
|
||||
#[attr1]
|
||||
mod m = "crate-attributes-src" {
|
||||
#[path = "crate-attributes-src"]
|
||||
mod m {
|
||||
#[attr_inner];
|
||||
|
||||
#[attr2]
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
mod multi = "multi-src" {
|
||||
#[path = "multi-src"]
|
||||
mod multi {
|
||||
// implicitly #[path = "foo.rs"]
|
||||
mod foo;
|
||||
|
||||
mod foo; // implicitly = "foo.rs"
|
||||
|
||||
mod bar = "bar.rs";
|
||||
#[path = "bar.rs"]
|
||||
mod bar;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue