Skip to content

Commit

Permalink
auto merge of #12030 : eddyb/rust/kill-at-self-and-trait, r=cmr
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Feb 6, 2014
2 parents c13a929 + b2d30b7 commit 6d27b01
Show file tree
Hide file tree
Showing 122 changed files with 627 additions and 1,694 deletions.
12 changes: 6 additions & 6 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ to pointers to the trait name, used as a type.
# impl Shape for int { }
# let mycircle = 0;
let myshape: @Shape = @mycircle as @Shape;
let myshape: ~Shape = ~mycircle as ~Shape;
~~~~

The resulting value is a managed box containing the value that was cast,
Expand Down Expand Up @@ -1396,7 +1396,7 @@ Likewise, supertrait methods may also be called on trait objects.
# impl Circle for int { fn radius(&self) -> f64 { 0.0 } }
# let mycircle = 0;
let mycircle: Circle = @mycircle as @Circle;
let mycircle: Circle = ~mycircle as ~Circle;
let nonsense = mycircle.radius() * mycircle.area();
~~~~

Expand Down Expand Up @@ -3290,8 +3290,8 @@ Whereas most calls to trait methods are "early bound" (statically resolved) to s
a call to a method on an object type is only resolved to a vtable entry at compile time.
The actual implementation for each vtable entry can vary on an object-by-object basis.

Given a pointer-typed expression `E` of type `&T`, `~T` or `@T`, where `T` implements trait `R`,
casting `E` to the corresponding pointer type `&R`, `~R` or `@R` results in a value of the _object type_ `R`.
Given a pointer-typed expression `E` of type `&T` or `~T`, where `T` implements trait `R`,
casting `E` to the corresponding pointer type `&R` or `~R` results in a value of the _object type_ `R`.
This result is represented as a pair of pointers:
the vtable pointer for the `T` implementation of `R`, and the pointer value of `E`.

Expand All @@ -3306,12 +3306,12 @@ impl Printable for int {
fn to_string(&self) -> ~str { self.to_str() }
}
fn print(a: @Printable) {
fn print(a: ~Printable) {
println!("{}", a.to_string());
}
fn main() {
print(@10 as @Printable);
print(~10 as ~Printable);
}
~~~~

Expand Down
27 changes: 10 additions & 17 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ like any other function, except for the name `self`.
The type of `self` is the type on which the method is implemented,
or a pointer thereof. As an argument it is written either `self`,
`&self`, `@self`, or `~self`.
`&self`, or `~self`.
A caller must in turn have a compatible pointer type to call the method.
~~~
Expand All @@ -1870,14 +1870,12 @@ A caller must in turn have a compatible pointer type to call the method.
# }
impl Shape {
fn draw_reference(&self) { ... }
fn draw_managed(@self) { ... }
fn draw_owned(~self) { ... }
fn draw_value(self) { ... }
}

let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);

(@s).draw_managed();
(~s).draw_owned();
(&s).draw_reference();
s.draw_value();
Expand All @@ -1897,7 +1895,6 @@ to a reference.
# }
# impl Shape {
# fn draw_reference(&self) { ... }
# fn draw_managed(@self) { ... }
# fn draw_owned(~self) { ... }
# fn draw_value(self) { ... }
# }
Expand Down Expand Up @@ -2368,29 +2365,29 @@ an _object_.
~~~~
# trait Drawable { fn draw(&self); }
fn draw_all(shapes: &[@Drawable]) {
fn draw_all(shapes: &[~Drawable]) {
for shape in shapes.iter() { shape.draw(); }
}
~~~~
In this example, there is no type parameter. Instead, the `@Drawable`
type denotes any managed box value that implements the `Drawable`
trait. To construct such a value, you use the `as` operator to cast a
value to an object:
In this example, there is no type parameter. Instead, the `~Drawable`
type denotes any owned box value that implements the `Drawable` trait.
To construct such a value, you use the `as` operator to cast a value
to an object:
~~~~
# type Circle = int; type Rectangle = bool;
# trait Drawable { fn draw(&self); }
# fn new_circle() -> Circle { 1 }
# fn new_rectangle() -> Rectangle { true }
# fn draw_all(shapes: &[@Drawable]) {}
# fn draw_all(shapes: &[~Drawable]) {}
impl Drawable for Circle { fn draw(&self) { ... } }
impl Drawable for Rectangle { fn draw(&self) { ... } }
let c: @Circle = @new_circle();
let r: @Rectangle = @new_rectangle();
draw_all([c as @Drawable, r as @Drawable]);
let c: ~Circle = ~new_circle();
let r: ~Rectangle = ~new_rectangle();
draw_all([c as ~Drawable, r as ~Drawable]);
~~~~
We omit the code for `new_circle` and `new_rectangle`; imagine that
Expand All @@ -2407,8 +2404,6 @@ for example, an `@Circle` may not be cast to an `~Drawable`.
# impl Drawable for int { fn draw(&self) {} }
# fn new_circle() -> int { 1 }
# fn new_rectangle() -> int { 2 }
// A managed object
let boxy: @Drawable = @new_circle() as @Drawable;
// An owned object
let owny: ~Drawable = ~new_circle() as ~Drawable;
// A borrowed object
Expand All @@ -2427,7 +2422,6 @@ particular set of built-in kinds that their contents must fulfill in
order to be packaged up in a trait object of that storage class.
* The contents of owned traits (`~Trait`) must fulfill the `Send` bound.
* The contents of managed traits (`@Trait`) must fulfill the `'static` bound.
* The contents of reference traits (`&Trait`) are not constrained by any bound.
Consequently, the trait objects themselves automatically fulfill their
Expand All @@ -2439,7 +2433,6 @@ to fulfilling `Send`, contents must also fulfill `Freeze`, and as a consequence,
the trait itself fulfills `Freeze`.
* `~Trait:Send` is equivalent to `~Trait`.
* `@Trait:'static` is equivalent to `@Trait`.
* `&Trait:` is equivalent to `&Trait`.
Builtin kind bounds can also be specified on closure types in the same way (for
Expand Down
71 changes: 30 additions & 41 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use syntax::attr;
use syntax::attr::{AttrMetaMethods};
use syntax::codemap;
use syntax::diagnostic;
use syntax::diagnostic::Emitter;
use syntax::ext::base::CrateLoader;
use syntax::parse;
use syntax::parse::token::InternedString;
Expand Down Expand Up @@ -136,10 +137,10 @@ pub fn build_configuration(sess: Session) ->
}

// Convert strings provided as --cfg [cfgspec] into a crate_cfg
fn parse_cfgspecs(cfgspecs: ~[~str], demitter: @diagnostic::Emitter)
fn parse_cfgspecs(cfgspecs: ~[~str])
-> ast::CrateConfig {
cfgspecs.move_iter().map(|s| {
let sess = parse::new_parse_sess(Some(demitter));
let sess = parse::new_parse_sess();
parse::parse_meta_from_source_str("cfgspec".to_str(), s, ~[], sess)
}).collect::<ast::CrateConfig>()
}
Expand Down Expand Up @@ -539,9 +540,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &Input,
phase_6_link_output(sess, &trans, &outputs);
}

struct IdentifiedAnnotation {
contents: (),
}
struct IdentifiedAnnotation;

impl pprust::PpAnn for IdentifiedAnnotation {
fn pre(&self, node: pprust::AnnNode) -> io::IoResult<()> {
Expand Down Expand Up @@ -619,18 +618,16 @@ pub fn pretty_print_input(sess: Session,
let annotation = match ppm {
PpmIdentified | PpmExpandedIdentified => {
@IdentifiedAnnotation {
contents: (),
} as @pprust::PpAnn
~IdentifiedAnnotation as ~pprust::PpAnn
}
PpmTyped => {
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
let analysis = phase_3_run_analysis_passes(sess, &crate, ast_map);
@TypedAnnotation {
~TypedAnnotation {
analysis: analysis
} as @pprust::PpAnn
} as ~pprust::PpAnn:
}
_ => @pprust::NoAnn as @pprust::PpAnn,
_ => ~pprust::NoAnn as ~pprust::PpAnn:,
};
let src = &sess.codemap.get_filemap(source_name(input)).src;
Expand Down Expand Up @@ -682,17 +679,15 @@ static architecture_abis : &'static [(&'static str, abi::Architecture)] = &'stat
("mips", abi::Mips)];
pub fn build_target_config(sopts: @session::Options,
demitter: @diagnostic::Emitter)
pub fn build_target_config(sopts: @session::Options)
-> @session::Config {
let os = match get_os(sopts.target_triple) {
Some(os) => os,
None => early_error(demitter, "unknown operating system")
None => early_error("unknown operating system")
};
let arch = match get_arch(sopts.target_triple) {
Some(arch) => arch,
None => early_error(demitter,
"unknown architecture: " + sopts.target_triple)
None => early_error("unknown architecture: " + sopts.target_triple)
};
let (int_type, uint_type) = match arch {
abi::X86 => (ast::TyI32, ast::TyU32),
Expand Down Expand Up @@ -730,8 +725,7 @@ pub fn host_triple() -> ~str {
}
pub fn build_session_options(binary: ~str,
matches: &getopts::Matches,
demitter: @diagnostic::Emitter)
matches: &getopts::Matches)
-> @session::Options {
let crate_types = matches.opt_strs("crate-type").flat_map(|s| {
s.split(',').map(|part| {
Expand All @@ -741,8 +735,7 @@ pub fn build_session_options(binary: ~str,
"staticlib" => session::CrateTypeStaticlib,
"dylib" => session::CrateTypeDylib,
"bin" => session::CrateTypeExecutable,
_ => early_error(demitter,
format!("unknown crate type: `{}`", part))
_ => early_error(format!("unknown crate type: `{}`", part))
}
}).collect()
});
Expand All @@ -767,8 +760,8 @@ pub fn build_session_options(binary: ~str,
let lint_name = lint_name.replace("-", "_");
match lint_dict.find_equiv(&lint_name) {
None => {
early_error(demitter, format!("unknown {} flag: {}",
level_name, lint_name));
early_error(format!("unknown {} flag: {}",
level_name, lint_name));
}
Some(lint) => {
lint_opts.push((lint.lint, *level));
Expand All @@ -787,7 +780,7 @@ pub fn build_session_options(binary: ~str,
if *name == *debug_flag { this_bit = bit; break; }
}
if this_bit == 0 {
early_error(demitter, format!("unknown debug flag: {}", *debug_flag))
early_error(format!("unknown debug flag: {}", *debug_flag))
}
debugging_opts |= this_bit;
}
Expand All @@ -807,9 +800,7 @@ pub fn build_session_options(binary: ~str,
"bc" => link::OutputTypeBitcode,
"obj" => link::OutputTypeObject,
"link" => link::OutputTypeExe,
_ => early_error(demitter,
format!("unknown emission type: `{}`",
part))
_ => early_error(format!("unknown emission type: `{}`", part))
}
}).collect()
})
Expand All @@ -830,7 +821,7 @@ pub fn build_session_options(binary: ~str,
No
} else if matches.opt_present("O") {
if matches.opt_present("opt-level") {
early_error(demitter, "-O and --opt-level both provided");
early_error("-O and --opt-level both provided");
}
Default
} else if matches.opt_present("opt-level") {
Expand All @@ -840,7 +831,7 @@ pub fn build_session_options(binary: ~str,
~"2" => Default,
~"3" => Aggressive,
_ => {
early_error(demitter, "optimization level needs to be between 0-3")
early_error("optimization level needs to be between 0-3")
}
}
} else { No }
Expand All @@ -865,7 +856,7 @@ pub fn build_session_options(binary: ~str,
}).collect()
});

let cfg = parse_cfgspecs(matches.opt_strs("cfg"), demitter);
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
let test = matches.opt_present("test");
let android_cross_path = matches.opt_str("android-cross-path");
let write_dependency_info = (matches.opt_present("dep-info"),
Expand Down Expand Up @@ -926,25 +917,23 @@ pub fn build_session_options(binary: ~str,
}

pub fn build_session(sopts: @session::Options,
local_crate_source_file: Option<Path>,
demitter: @diagnostic::Emitter)
local_crate_source_file: Option<Path>)
-> Session {
let codemap = @codemap::CodeMap::new();
let diagnostic_handler =
diagnostic::mk_handler(Some(demitter));
diagnostic::mk_handler();
let span_diagnostic_handler =
diagnostic::mk_span_handler(diagnostic_handler, codemap);

build_session_(sopts, local_crate_source_file, codemap, demitter, span_diagnostic_handler)
build_session_(sopts, local_crate_source_file, codemap, span_diagnostic_handler)
}

pub fn build_session_(sopts: @session::Options,
local_crate_source_file: Option<Path>,
codemap: @codemap::CodeMap,
demitter: @diagnostic::Emitter,
span_diagnostic_handler: @diagnostic::SpanHandler)
-> Session {
let target_cfg = build_target_config(sopts, demitter);
let target_cfg = build_target_config(sopts);
let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler, codemap);
let cstore = @CStore::new(token::get_ident_interner());
let filesearch = @filesearch::FileSearch::new(
Expand Down Expand Up @@ -1167,8 +1156,8 @@ pub fn build_output_filenames(input: &Input,
}
}

pub fn early_error(emitter: &diagnostic::Emitter, msg: &str) -> ! {
emitter.emit(None, msg, diagnostic::Fatal);
pub fn early_error(msg: &str) -> ! {
diagnostic::DefaultEmitter.emit(None, msg, diagnostic::Fatal);
fail!(diagnostic::FatalError);
}

Expand Down Expand Up @@ -1198,8 +1187,8 @@ mod test {
Ok(m) => m,
Err(f) => fail!("test_switch_implies_cfg_test: {}", f.to_err_msg())
};
let sessopts = build_session_options(~"rustc", matches, @diagnostic::DefaultEmitter);
let sess = build_session(sessopts, None, @diagnostic::DefaultEmitter);
let sessopts = build_session_options(~"rustc", matches);
let sess = build_session(sessopts, None);
let cfg = build_configuration(sess);
assert!((attr::contains_name(cfg, "test")));
}
Expand All @@ -1216,8 +1205,8 @@ mod test {
f.to_err_msg());
}
};
let sessopts = build_session_options(~"rustc", matches, @diagnostic::DefaultEmitter);
let sess = build_session(sessopts, None, @diagnostic::DefaultEmitter);
let sessopts = build_session_options(~"rustc", matches);
let sess = build_session(sessopts, None);
let cfg = build_configuration(sess);
let mut test_items = cfg.iter().filter(|m| m.name().equiv(&("test")));
assert!(test_items.next().is_some());
Expand Down
Loading

0 comments on commit 6d27b01

Please sign in to comment.