Skip to content

Commit c3825c8

Browse files
committed
env! syntax extension changes
env! aborts compilation of the specified environment variable is not defined and takes an optional second argument containing a custom error message. option_env! creates an Option<&'static str> containing the value of the environment variable. There are no run-pass tests that check the behavior when the environment variable is defined since the test framework doesn't support setting environment variables at compile time as opposed to runtime. However, both env! and option_env! are used inside of rustc itself, which should act as a sufficient test. Close #2248
1 parent 9db698a commit c3825c8

19 files changed

+168
-32
lines changed

src/librustc/back/rpath.rs

+9
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub fn get_absolute_rpath(lib: &Path) -> Path {
168168
os::make_absolute(lib).dir_path()
169169
}
170170

171+
#[cfg(stage0)]
171172
pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
172173
let install_prefix = env!("CFG_PREFIX");
173174

@@ -179,6 +180,14 @@ pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
179180
os::make_absolute(&Path(install_prefix).push_rel(&tlib))
180181
}
181182

183+
#[cfg(not(stage0))]
184+
pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
185+
let install_prefix = env!("CFG_PREFIX");
186+
187+
let tlib = filesearch::relative_target_lib_path(target_triple);
188+
os::make_absolute(&Path(install_prefix).push_rel(&tlib))
189+
}
190+
182191
pub fn minimize_rpaths(rpaths: &[Path]) -> ~[Path] {
183192
let mut set = HashSet::new();
184193
let mut minimized = ~[];

src/librustc/driver/driver.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -578,6 +578,7 @@ pub fn build_target_config(sopts: @session::options,
578578
return target_cfg;
579579
}
580580
581+
#[cfg(stage0)]
581582
pub fn host_triple() -> ~str {
582583
// Get the host triple out of the build environment. This ensures that our
583584
// idea of the host triple is the same as for the set of libraries we've
@@ -595,6 +596,19 @@ pub fn host_triple() -> ~str {
595596
};
596597
}
597598

599+
#[cfg(not(stage0))]
600+
pub fn host_triple() -> ~str {
601+
// Get the host triple out of the build environment. This ensures that our
602+
// idea of the host triple is the same as for the set of libraries we've
603+
// actually built. We can't just take LLVM's host triple because they
604+
// normalize all ix86 architectures to i386.
605+
//
606+
// Instead of grabbing the host triple (for the current host), we grab (at
607+
// compile time) the target triple that this rustc is built with and
608+
// calling that (at runtime) the host triple.
609+
(env!("CFG_COMPILER_TRIPLE")).to_owned()
610+
}
611+
598612
pub fn build_session_options(binary: @str,
599613
matches: &getopts::Matches,
600614
demitter: diagnostic::Emitter)

src/librustc/metadata/filesearch.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -186,10 +186,16 @@ fn get_rustpkg_lib_path_nearest() -> Result<Path, ~str> {
186186

187187
// The name of the directory rustc expects libraries to be located.
188188
// On Unix should be "lib", on windows "bin"
189+
#[cfg(stage0)]
189190
pub fn libdir() -> ~str {
190191
let libdir = env!("CFG_LIBDIR");
191192
if libdir.is_empty() {
192193
fail!("rustc compiled without CFG_LIBDIR environment variable");
193194
}
194195
libdir.to_owned()
195196
}
197+
198+
#[cfg(not(stage0))]
199+
pub fn libdir() -> ~str {
200+
(env!("CFG_LIBDIR")).to_owned()
201+
}

src/librustc/middle/resolve.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -4991,7 +4991,9 @@ impl Resolver {
49914991
if self.structs.contains(&class_id) => {
49924992
self.record_def(expr.id, definition);
49934993
}
4994-
_ => {
4994+
result => {
4995+
debug!("(resolving expression) didn't find struct \
4996+
def: %?", result);
49954997
self.session.span_err(
49964998
path.span,
49974999
fmt!("`%s` does not name a structure",

src/librustc/rustc.rs

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ mod std {
117117
}
118118
*/
119119

120+
#[cfg(stage0)]
120121
pub fn version(argv0: &str) {
121122
let mut vers = ~"unknown version";
122123
let env_vers = env!("CFG_VERSION");
@@ -125,6 +126,16 @@ pub fn version(argv0: &str) {
125126
printfln!("host: %s", host_triple());
126127
}
127128

129+
#[cfg(not(stage0))]
130+
pub fn version(argv0: &str) {
131+
let vers = match option_env!("CFG_VERSION") {
132+
Some(vers) => vers,
133+
None => "unknown version"
134+
};
135+
printfln!("%s %s", argv0, vers);
136+
printfln!("host: %s", host_triple());
137+
}
138+
128139
pub fn usage(argv0: &str) {
129140
let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
130141
printfln!("%s\

src/libsyntax/ext/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -59,7 +59,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
5959
match state {
6060
Asm => {
6161
asm = expr_to_str(cx, p.parse_expr(),
62-
~"inline assembly must be a string literal.");
62+
"inline assembly must be a string literal.");
6363
}
6464
Outputs => {
6565
while *p.token != token::EOF &&

src/libsyntax/ext/base.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
146146
intern(&"auto_decode"),
147147
@SE(ItemDecorator(ext::auto_encode::expand_auto_decode)));
148148
syntax_expanders.insert(intern(&"env"),
149-
builtin_normal_tt(ext::env::expand_syntax_ext));
149+
builtin_normal_tt(ext::env::expand_env));
150+
syntax_expanders.insert(intern(&"option_env"),
151+
builtin_normal_tt(ext::env::expand_option_env));
150152
syntax_expanders.insert(intern("bytes"),
151153
builtin_normal_tt(ext::bytes::expand_syntax_ext));
152154
syntax_expanders.insert(intern("concat_idents"),
@@ -311,7 +313,7 @@ impl ExtCtxt {
311313
}
312314
}
313315

314-
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: ~str) -> @str {
316+
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: &str) -> @str {
315317
match expr.node {
316318
ast::expr_lit(l) => match l.node {
317319
ast::lit_str(s) => s,
@@ -536,8 +538,8 @@ mod test {
536538
a.insert (@"abc",@15);
537539
let m = MapChain::new(~a);
538540
m.insert (@"def",@16);
539-
// FIXME: #4492 (ICE) assert_eq!(m.find(&@"abc"),Some(@15));
540-
// .... assert_eq!(m.find(&@"def"),Some(@16));
541+
assert_eq!(m.find(&@"abc"),Some(@15));
542+
assert_eq!(m.find(&@"def"),Some(@16));
541543
assert_eq!(*(m.find(&@"abc").unwrap()),15);
542544
assert_eq!(*(m.find(&@"def").unwrap()),16);
543545
let n = m.push_frame();
@@ -549,8 +551,8 @@ mod test {
549551
assert_eq!(*(n.find(&@"abc").unwrap()),15);
550552
assert_eq!(*(n.find(&@"def").unwrap()),17);
551553
// ... but m still has the old ones
552-
// FIXME: #4492: assert_eq!(m.find(&@"abc"),Some(@15));
553-
// FIXME: #4492: assert_eq!(m.find(&@"def"),Some(@16));
554+
assert_eq!(m.find(&@"abc"),Some(@15));
555+
assert_eq!(m.find(&@"def"),Some(@16));
554556
assert_eq!(*(m.find(&@"abc").unwrap()),15);
555557
assert_eq!(*(m.find(&@"def").unwrap()),16);
556558
}

src/libsyntax/ext/env.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -22,17 +22,35 @@ use ext::build::AstBuilder;
2222

2323
use std::os;
2424

25-
pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
25+
pub fn expand_option_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
2626
-> base::MacResult {
27+
let var = get_single_str_from_tts(ext_cx, sp, tts, "option_env!");
2728

28-
let var = get_single_str_from_tts(cx, sp, tts, "env!");
29+
let e = match os::getenv(var) {
30+
None => quote_expr!(::std::option::None),
31+
Some(s) => quote_expr!(::std::option::Some($s))
32+
};
33+
MRExpr(e)
34+
}
2935

30-
// FIXME (#2248): if this was more thorough it would manufacture an
31-
// Option<str> rather than just an maybe-empty string.
36+
pub fn expand_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
37+
-> base::MacResult {
38+
let exprs = get_exprs_from_tts(ext_cx, sp, tts);
39+
40+
if exprs.len() == 0 {
41+
ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments");
42+
}
43+
44+
let var = expr_to_str(ext_cx, exprs[0], "expected string literal");
45+
let msg = match exprs.len() {
46+
1 => fmt!("Environment variable %s not defined", var).to_managed(),
47+
2 => expr_to_str(ext_cx, exprs[1], "expected string literal"),
48+
_ => ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments")
49+
};
3250

3351
let e = match os::getenv(var) {
34-
None => cx.expr_str(sp, @""),
35-
Some(s) => cx.expr_str(sp, s.to_managed())
52+
None => ext_cx.span_fatal(sp, msg),
53+
Some(s) => ext_cx.expr_str(sp, s.to_managed())
3654
};
3755
MRExpr(e)
3856
}

src/libsyntax/ext/fmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -32,7 +32,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
3232
}
3333
let fmt =
3434
expr_to_str(cx, args[0],
35-
~"first argument to fmt! must be a string literal.");
35+
"first argument to fmt! must be a string literal.");
3636
let fmtspan = args[0].span;
3737
debug!("Format string: %s", fmt);
3838
fn parse_fmt_err_(cx: @ExtCtxt, sp: span, msg: &str) -> ! {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("one", 10); } //~ ERROR: expected string literal
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: env! takes 1 argument
12-
13-
fn main() { env!(); }
11+
fn main() { env!(); } //~ ERROR: env! takes 1 or 2 arguments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("__HOPEFULLY_NOT_DEFINED__", "my error message"); } //~ ERROR: my error message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("__HOPEFULLY_NOT_DEFINED__"); } //~ ERROR: Environment variable __HOPEFULLY_NOT_DEFINED__ not defined
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:requires a string
12-
13-
fn main() { env!(10); }
11+
fn main() { env!(10, "two"); } //~ ERROR: expected string literal

src/test/compile-fail/extenv-too-many-args.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: env! takes 1 argument
12-
13-
fn main() { env!("one", "two"); }
11+
fn main() { env!("one", "two", "three"); } //~ ERROR: env! takes 1 or 2 arguments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!(); } //~ ERROR: option_env! takes 1 argument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!(10); } //~ ERROR: requires a string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!("one", "two"); } //~ ERROR: option_env! takes 1 argument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let opt: Option<&'static str> = option_env!("__HOPEFULLY_DOESNT_EXIST__");
13+
assert!(opt.is_none());
14+
}

0 commit comments

Comments
 (0)