Skip to content

Commit dd86f9d

Browse files
committed
auto merge of #17197 : nikomatsakis/rust/issue-5527-trait-reform-revisited, r=pcwalton
This patch does not make many functional changes, but does a lot of restructuring towards the goals of #5527. This is the biggest patch, basically, that should enable most of the other patches in a relatively straightforward way. Major changes: - Do not track impls through trans, instead recompute as needed. - Isolate trait matching code into its own module, carefully structure to distinguish various phases (selection vs confirmation vs fulfillment) - Consider where clauses in their more general form - Integrate checking of builtin bounds into the trait matching process, rather than doing it separately in kind.rs (important for opt-in builtin bounds) What is not included: - Where clauses are still not generalized. This should be a straightforward follow-up patch. - Caching. I did not include much caching. I have plans for various kinds of caching we can do. Should be straightforward. Preliminary perf measurements suggested that this branch keeps compilation times roughly what they are. - Method resolution. The initial algorithm I proposed for #5527 does not work as well as I hoped. I have a revised plan which is much more similar to what we do today. - Deref vs deref-mut. The initial fix I had worked great for autoderef, but not for explicit deref. - Permitting blanket impls to overlap with specific impls. Initial plan to consider all nested obligations before considering an impl to match caused many compilation errors. We have a revised plan but it is not implemented here, should be a relatively straightforward extension.
2 parents 3212d70 + eafeb33 commit dd86f9d

File tree

142 files changed

+5676
-3042
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+5676
-3042
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5959
TOOLS := compiletest rustdoc rustc
6060

6161
DEPS_core :=
62-
DEPS_rlibc :=
62+
DEPS_rlibc := core
6363
DEPS_unicode := core
6464
DEPS_alloc := core libc native:jemalloc
6565
DEPS_debug := std

src/doc/guide-unsafe.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,12 @@ fn start(_argc: int, _argv: *const *const u8) -> int {
461461
0
462462
}
463463
464-
// These functions are invoked by the compiler, but not
464+
// These functions and traits are used by the compiler, but not
465465
// for a bare-bones hello world. These are normally
466466
// provided by libstd.
467467
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
468468
#[lang = "eh_personality"] extern fn eh_personality() {}
469+
#[lang = "sized"] trait Sized { }
469470
# // fn main() {} tricked you, rustdoc!
470471
```
471472

@@ -488,13 +489,14 @@ pub extern fn main(argc: int, argv: *const *const u8) -> int {
488489
489490
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
490491
#[lang = "eh_personality"] extern fn eh_personality() {}
492+
#[lang = "sized"] trait Sized { }
491493
# // fn main() {} tricked you, rustdoc!
492494
```
493495

494496

495497
The compiler currently makes a few assumptions about symbols which are available
496498
in the executable to call. Normally these functions are provided by the standard
497-
library, but without it you must define your own.
499+
xlibrary, but without it you must define your own.
498500

499501
The first of these two functions, `stack_exhausted`, is invoked whenever stack
500502
overflow is detected. This function has a number of restrictions about how it
@@ -508,6 +510,12 @@ mechanisms of the compiler. This is often mapped to GCC's personality function
508510
information), but crates which do not trigger failure can be assured that this
509511
function is never called.
510512

513+
The final item in the example is a trait called `Sized`. This a trait
514+
that represents data of a known static size: it is integral to the
515+
Rust type system, and so the compiler expects the standard library to
516+
provide it. Since you are not using the standard library, you have to
517+
provide it yourself.
518+
511519
## Using libcore
512520

513521
> **Note**: the core library's structure is unstable, and it is recommended to
@@ -686,6 +694,7 @@ fn main(argc: int, argv: *const *const u8) -> int {
686694
687695
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
688696
#[lang = "eh_personality"] extern fn eh_personality() {}
697+
#[lang = "sized"] trait Sized {}
689698
```
690699

691700
Note the use of `abort`: the `exchange_malloc` lang item is assumed to

src/liballoc/heap.rs

-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ mod imp {
238238
#[cfg(not(jemalloc), unix)]
239239
mod imp {
240240
use core::cmp;
241-
use core::mem;
242241
use core::ptr;
243242
use libc;
244243
use libc_heap;

src/librlibc/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
// LLVM to optimize these function calls to themselves!
3636
#![no_builtins]
3737

38+
#[phase(plugin, link)] extern crate core;
39+
3840
#[cfg(test)] extern crate native;
3941
#[cfg(test)] extern crate test;
4042
#[cfg(test)] extern crate debug;
4143

4244
#[cfg(test)] #[phase(plugin, link)] extern crate std;
43-
#[cfg(test)] #[phase(plugin, link)] extern crate core;
4445

4546
// Require the offset intrinsics for LLVM to properly optimize the
4647
// implementations below. If pointer arithmetic is done through integers the

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub mod middle {
108108
pub mod save;
109109
pub mod stability;
110110
pub mod subst;
111+
pub mod traits;
111112
pub mod trans;
112113
pub mod ty;
113114
pub mod ty_fold;

src/librustc/lint/builtin.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1470,17 +1470,17 @@ impl LintPass for Stability {
14701470
def_id
14711471
}
14721472
typeck::MethodParam(typeck::MethodParam {
1473-
trait_id: trait_id,
1473+
trait_ref: ref trait_ref,
14741474
method_num: index,
14751475
..
1476-
})
1477-
| typeck::MethodObject(typeck::MethodObject {
1478-
trait_id: trait_id,
1476+
}) |
1477+
typeck::MethodObject(typeck::MethodObject {
1478+
trait_ref: ref trait_ref,
14791479
method_num: index,
14801480
..
14811481
}) => {
14821482
match ty::trait_item(cx.tcx,
1483-
trait_id,
1483+
trait_ref.def_id,
14841484
index) {
14851485
ty::MethodTraitItem(method) => {
14861486
method.def_id

src/librustc/metadata/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ pub enum astencode_tag { // Reserves 0x40 -- 0x5f
140140
tag_table_unboxed_closures = 0x54,
141141
tag_table_upvar_borrow_map = 0x55,
142142
tag_table_capture_modes = 0x56,
143+
tag_table_object_cast_map = 0x57,
143144
}
144145
static first_astencode_tag: uint = tag_ast as uint;
145-
static last_astencode_tag: uint = tag_table_capture_modes as uint;
146+
static last_astencode_tag: uint = tag_table_object_cast_map as uint;
146147
impl astencode_tag {
147148
pub fn from_uint(value : uint) -> Option<astencode_tag> {
148149
let is_a_tag = first_astencode_tag <= value && value <= last_astencode_tag;

src/librustc/metadata/encoder.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use metadata::cstore;
2020
use metadata::decoder;
2121
use metadata::tyencode;
2222
use middle::ty::{lookup_item_type};
23-
use middle::astencode;
2423
use middle::ty;
25-
use middle::typeck;
2624
use middle::stability;
2725
use middle;
2826
use util::nodemap::{NodeMap, NodeSet};
@@ -125,14 +123,6 @@ fn encode_trait_ref(rbml_w: &mut Encoder,
125123
rbml_w.end_tag();
126124
}
127125

128-
fn encode_impl_vtables(rbml_w: &mut Encoder,
129-
ecx: &EncodeContext,
130-
vtables: &typeck::vtable_res) {
131-
rbml_w.start_tag(tag_item_impl_vtables);
132-
astencode::encode_vtable_res(ecx, rbml_w, vtables);
133-
rbml_w.end_tag();
134-
}
135-
136126
// Item info table encoding
137127
fn encode_family(rbml_w: &mut Encoder, c: char) {
138128
rbml_w.start_tag(tag_items_data_item_family);
@@ -191,6 +181,18 @@ pub fn write_type(ecx: &EncodeContext,
191181
tyencode::enc_ty(rbml_w.writer, ty_str_ctxt, typ);
192182
}
193183

184+
pub fn write_trait_ref(ecx: &EncodeContext,
185+
rbml_w: &mut Encoder,
186+
trait_ref: &ty::TraitRef) {
187+
let ty_str_ctxt = &tyencode::ctxt {
188+
diag: ecx.diag,
189+
ds: def_to_string,
190+
tcx: ecx.tcx,
191+
abbrevs: &ecx.type_abbrevs
192+
};
193+
tyencode::enc_trait_ref(rbml_w.writer, ty_str_ctxt, trait_ref);
194+
}
195+
194196
pub fn write_region(ecx: &EncodeContext,
195197
rbml_w: &mut Encoder,
196198
r: ty::Region) {
@@ -399,7 +401,7 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
399401
let impl_items = ecx.tcx.impl_items.borrow();
400402
match ecx.tcx.inherent_impls.borrow().find(&exp.def_id) {
401403
Some(implementations) => {
402-
for base_impl_did in implementations.borrow().iter() {
404+
for base_impl_did in implementations.iter() {
403405
for &method_did in impl_items.get(base_impl_did).iter() {
404406
let impl_item = ty::impl_or_trait_item(
405407
ecx.tcx,
@@ -946,7 +948,7 @@ fn encode_inherent_implementations(ecx: &EncodeContext,
946948
match ecx.tcx.inherent_impls.borrow().find(&def_id) {
947949
None => {}
948950
Some(implementations) => {
949-
for &impl_def_id in implementations.borrow().iter() {
951+
for &impl_def_id in implementations.iter() {
950952
rbml_w.start_tag(tag_items_data_item_inherent_impl);
951953
encode_def_id(rbml_w, impl_def_id);
952954
rbml_w.end_tag();
@@ -1203,8 +1205,6 @@ fn encode_info_for_item(ecx: &EncodeContext,
12031205
let trait_ref = ty::node_id_to_trait_ref(
12041206
tcx, ast_trait_ref.ref_id);
12051207
encode_trait_ref(rbml_w, ecx, &*trait_ref, tag_item_trait_ref);
1206-
let impl_vtables = ty::lookup_impl_vtables(tcx, def_id);
1207-
encode_impl_vtables(rbml_w, ecx, &impl_vtables);
12081208
}
12091209
encode_path(rbml_w, path.clone());
12101210
encode_stability(rbml_w, stab);

0 commit comments

Comments
 (0)