Skip to content

Add a path attribute for crate directive #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/rust.texi
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,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

Expand Down
3 changes: 2 additions & 1 deletion src/comp/rustc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/comp/syntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
cdir_auth(path, _auth);
Expand Down
8 changes: 4 additions & 4 deletions src/comp/syntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
20 changes: 14 additions & 6 deletions src/comp/syntax/parse/eval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import front::attr;
import std::{option, result, io, fs};
import std::option::{some, none};
import syntax::ast;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/comp/syntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2502,17 +2502,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)) }
_ { none }
};
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. {
Expand All @@ -2525,7 +2520,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); }
}
Expand Down
4 changes: 2 additions & 2 deletions src/comp/syntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 14 additions & 7 deletions src/lib/std.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -99,19 +100,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;


// FIXME: This doesn't do anything.
Expand Down
3 changes: 0 additions & 3 deletions src/test/compile-fail/mod-name-non-str.rc

This file was deleted.

8 changes: 5 additions & 3 deletions src/test/run-pass/companionmod.rc
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/test/run-pass/crate-attributes.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#[vers = "1.0"];

#[attr1]
mod m = "crate-attributes-src" {
#[path = "crate-attributes-src"]
mod m {
#[attr_inner];

#[attr2]
Expand Down
10 changes: 6 additions & 4 deletions src/test/run-pass/multi.rc
Original file line number Diff line number Diff line change
@@ -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;
}