Skip to content

Commit 1c41b77

Browse files
authored
Merge pull request #66 from orium/make-it-work-again
Make it work again :)
2 parents a5a05d3 + 6abeba7 commit 1c41b77

File tree

10 files changed

+172
-165
lines changed

10 files changed

+172
-165
lines changed

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ name = "rust-semverver"
2525
path = "src/bin/rust_semverver.rs"
2626

2727
[dependencies]
28-
cargo = "^0.27"
29-
crates-io = "^0.16"
30-
env_logger = "^0.5"
31-
log = "^0.4"
32-
semver = "^0.9"
28+
cargo = "0.29"
29+
crates-io = "0.17"
30+
env_logger = "0.5"
31+
log = "0.4"
32+
semver = "0.9"
33+
rand = "0.5"
3334

3435
[dev-dependencies]
35-
quickcheck = "0.6"
36+
quickcheck = "0.7"

src/bin/cargo_semver.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crates_io::{Crate, Registry};
1212

1313
use cargo::exit_with_error;
1414
use cargo::core::{Package, PackageId, Source, SourceId, Workspace};
15-
use cargo::ops::{compile, CompileMode, CompileOptions};
15+
use cargo::core::compiler::CompileMode;
16+
use cargo::ops::{compile, CompileOptions};
1617
use cargo::util::{CargoError, CargoResult, CliError};
1718
use cargo::util::config::Config;
1819
use cargo::util::important_paths::find_root_manifest_for_wd;
@@ -141,7 +142,7 @@ impl<'a> WorkInfo<'a> {
141142
fn rlib_and_dep_output(&self, config: &'a Config, name: &str, current: bool)
142143
-> CargoResult<(PathBuf, PathBuf)>
143144
{
144-
let opts = CompileOptions::default(config, CompileMode::Build);
145+
let opts = CompileOptions::new(config, CompileMode::Build).unwrap();
145146

146147
if current {
147148
env::set_var("RUSTFLAGS", "-C metadata=new");
@@ -247,10 +248,12 @@ fn do_main(config: &Config, matches: &Matches, explain: bool) -> CargoResult<()>
247248
.map_err(|e| Error(format!("could not spawn rustc: {}", e)))?;
248249

249250
if let Some(ref mut stdin) = child.stdin {
251+
// The order of the `extern crate` declaration is important here: it will later
252+
// be used to select the `old` and `new` crates.
250253
stdin.write_fmt(format_args!("#[allow(unused_extern_crates)] \
251-
extern crate new; \
254+
extern crate old; \
252255
#[allow(unused_extern_crates)] \
253-
extern crate old;"))?;
256+
extern crate new;"))?;
254257
} else {
255258
return Err(Error("could not pipe to rustc (wtf?)".to_owned()).into());
256259
}

src/bin/rust_semverver.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc::hir::def_id::*;
1919
use rustc_metadata::cstore::CStore;
2020
use rustc::session::{config, Session};
2121
use rustc::session::config::{Input, ErrorOutputType};
22+
use rustc::middle::cstore::ExternCrate;
2223

2324
use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
2425

@@ -28,46 +29,46 @@ use std::path::PathBuf;
2829
use std::process::Command;
2930

3031
use syntax::ast;
32+
use syntax::source_map::Pos;
3133

3234
/// After the typechecker has finished it's work, perform our checks.
33-
///
34-
/// To compare the two well-typed crates, first find the aptly named crates `new` and `old`,
35-
/// find their root modules and then proceed to walk their module trees.
3635
fn callback(state: &driver::CompileState, version: &str, verbose: bool) {
3736
let tcx = state.tcx.unwrap();
3837

39-
let cnums = tcx
38+
// To select the old and new crates we look at the position of the declaration in the
39+
// source file. The first one will be the `old` and the other will be `new`. This is
40+
// unfortunately a bit hacky... See issue #64 for details.
41+
42+
let mut crates: Vec<_> = tcx
4043
.crates()
4144
.iter()
42-
.fold((None, None), |(o, n), crate_num| {
43-
let name = tcx.crate_name(*crate_num);
44-
if name == "old" {
45-
(Some(*crate_num), n)
46-
} else if name == "new" {
47-
(o, Some(*crate_num))
48-
} else {
49-
(o, n)
45+
.flat_map(|crate_num| {
46+
let def_id = DefId {
47+
krate: *crate_num,
48+
index: CRATE_DEF_INDEX,
49+
};
50+
51+
match *tcx.extern_crate(def_id) {
52+
Some(ExternCrate { span, direct: true, ..}) if span.data().lo.to_usize() > 0 =>
53+
Some((span.data().lo.to_usize(), def_id)),
54+
_ => None,
5055
}
51-
});
52-
53-
let (old_def_id, new_def_id) = if let (Some(c0), Some(c1)) = cnums {
54-
(DefId {
55-
krate: c0,
56-
index: CRATE_DEF_INDEX,
57-
},
58-
DefId {
59-
krate: c1,
60-
index: CRATE_DEF_INDEX,
61-
})
62-
} else {
63-
tcx.sess.err("could not find crate `old` and/or `new`");
64-
return;
65-
};
56+
})
57+
.collect();
58+
59+
crates.sort_by_key(|&(span_lo, _)| span_lo);
6660

67-
debug!("running semver analysis");
68-
let changes = run_analysis(tcx, old_def_id, new_def_id);
61+
match crates.as_slice() {
62+
&[(_, old_def_id), (_, new_def_id)] => {
63+
debug!("running semver analysis");
64+
let changes = run_analysis(tcx, old_def_id, new_def_id);
6965

70-
changes.output(tcx.sess, version, verbose);
66+
changes.output(tcx.sess, version, verbose);
67+
}
68+
_ => {
69+
tcx.sess.err("could not find crate old and new crates");
70+
}
71+
}
7172
}
7273

7374
/// A wrapper to control compilation.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
extern crate log;
66

77
#[cfg(test)]
8-
#[macro_use]
98
extern crate quickcheck;
109

10+
extern crate rand;
1111
extern crate rustc;
1212
extern crate semver;
1313
extern crate syntax;

src/semcheck/changes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ pub mod tests {
948948
impl Arbitrary for ChangeType_ {
949949
fn arbitrary<G: Gen>(g: &mut G) -> ChangeType_ {
950950
use self::ChangeType_::*;
951+
use ::rand::Rng;
951952

952953
let b1 = Arbitrary::arbitrary(g);
953954
let b2 = Arbitrary::arbitrary(g);

src/semcheck/mapping.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl NameMapping {
315315
Trait(_) |
316316
Existential(_) |
317317
TyAlias(_) |
318-
TyForeign(_) |
318+
ForeignTy(_) |
319319
TraitAlias(_) | // TODO: will need some handling later on.
320320
AssociatedTy(_) |
321321
AssociatedExistential(_) |
@@ -327,6 +327,7 @@ impl NameMapping {
327327
Const(_) |
328328
Static(_, _) |
329329
StructCtor(_, _) |
330+
SelfCtor(_) |
330331
VariantCtor(_, _) |
331332
Method(_) |
332333
AssociatedConst(_) |

src/semcheck/mismatch.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for MismatchRelation<'a, 'gcx,
107107
}
108108

109109
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
110-
use rustc::ty::TypeVariants::*;
110+
use rustc::ty::TyKind;
111111

112112
if self.current_old_types.contains(a) || self.current_new_types.contains(b) {
113113
return Ok(self.tcx.types.err);
@@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for MismatchRelation<'a, 'gcx,
118118

119119
debug!("tys: mismatch relation: a: {:?}, b: {:?}", a, b);
120120
let matching = match (&a.sty, &b.sty) {
121-
(&TyAdt(a_def, a_substs), &TyAdt(b_def, b_substs)) => {
121+
(&TyKind::Adt(a_def, a_substs), &TyKind::Adt(b_def, b_substs)) => {
122122
if self.check_substs(a_substs, b_substs) {
123123
let _ = self.relate_item_substs(a_def.did, a_substs, b_substs)?;
124124
let a_adt = self.tcx.adt_def(a_def.did);
@@ -148,21 +148,21 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for MismatchRelation<'a, 'gcx,
148148
None
149149
}
150150
},
151-
(&TyArray(a_t, _), &TyArray(b_t, _)) |
152-
(&TySlice(a_t), &TySlice(b_t)) => {
151+
(&TyKind::Array(a_t, _), &TyKind::Array(b_t, _)) |
152+
(&TyKind::Slice(a_t), &TyKind::Slice(b_t)) => {
153153
let _ = self.relate(&a_t, &b_t)?;
154154
None
155155
},
156-
(&TyRawPtr(a_mt), &TyRawPtr(b_mt)) => {
156+
(&TyKind::RawPtr(a_mt), &TyKind::RawPtr(b_mt)) => {
157157
let _ = self.relate(&a_mt, &b_mt)?;
158158
None
159159
},
160-
(&TyRef(a_r, a_ty, _), &TyRef(b_r, b_ty, _)) => {
160+
(&TyKind::Ref(a_r, a_ty, _), &TyKind::Ref(b_r, b_ty, _)) => {
161161
let _ = self.relate(&a_r, &b_r)?;
162162
let _ = self.relate(&a_ty, &b_ty)?;
163163
None
164164
},
165-
(&TyFnDef(a_def_id, a_substs), &TyFnDef(b_def_id, b_substs)) => {
165+
(&TyKind::FnDef(a_def_id, a_substs), &TyKind::FnDef(b_def_id, b_substs)) => {
166166
if self.check_substs(a_substs, b_substs) {
167167
let a_sig = a.fn_sig(self.tcx);
168168
let b_sig = b.fn_sig(self.tcx);
@@ -172,11 +172,11 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for MismatchRelation<'a, 'gcx,
172172

173173
Some((a_def_id, b_def_id))
174174
},
175-
(&TyFnPtr(a_fty), &TyFnPtr(b_fty)) => {
175+
(&TyKind::FnPtr(a_fty), &TyKind::FnPtr(b_fty)) => {
176176
let _ = self.relate(&a_fty, &b_fty)?;
177177
None
178178
},
179-
(&TyDynamic(a_obj, a_r), &TyDynamic(b_obj, b_r)) => {
179+
(&TyKind::Dynamic(a_obj, a_r), &TyKind::Dynamic(b_obj, b_r)) => {
180180
let _ = self.relate(&a_r, &b_r)?;
181181

182182
match (a_obj.principal(), b_obj.principal()) {
@@ -188,22 +188,22 @@ impl<'a, 'gcx, 'tcx> TypeRelation<'a, 'gcx, 'tcx> for MismatchRelation<'a, 'gcx,
188188
_ => None,
189189
}
190190
},
191-
(&TyTuple(as_), &TyTuple(bs)) => {
191+
(&TyKind::Tuple(as_), &TyKind::Tuple(bs)) => {
192192
let _ = as_.iter().zip(bs).map(|(a, b)| self.relate(a, b));
193193
None
194194
},
195-
(&TyProjection(a_data), &TyProjection(b_data)) => {
195+
(&TyKind::Projection(a_data), &TyKind::Projection(b_data)) => {
196196
let _ = self.relate(&a_data, &b_data)?;
197197
Some((a_data.item_def_id, b_data.item_def_id))
198198
},
199-
(&TyAnon(a_def_id, a_substs), &TyAnon(b_def_id, b_substs)) => {
199+
(&TyKind::Opaque(a_def_id, a_substs), &TyKind::Opaque(b_def_id, b_substs)) => {
200200
if self.check_substs(a_substs, b_substs) {
201201
let _ = ty::relate::relate_substs(self, None, a_substs, b_substs)?;
202202
}
203203

204204
Some((a_def_id, b_def_id))
205205
},
206-
(&TyInfer(_), _) | (_, &TyInfer(_)) => {
206+
(&TyKind::Infer(_), _) | (_, &TyKind::Infer(_)) => {
207207
// As the original function this is ripped off of, we don't handle these cases.
208208
panic!("var types encountered in MismatchRelation::tys")
209209
},

src/semcheck/translate.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
164164
use rustc::ty::{AdtDef, Binder, ExistentialProjection, ExistentialTraitRef};
165165
use rustc::ty::ExistentialPredicate::*;
166166
use rustc::ty::TypeAndMut;
167-
use rustc::ty::TypeVariants::*;
167+
use rustc::ty::TyKind;
168168

169169
orig.fold_with(&mut BottomUpFolder { tcx: self.tcx, fldop: |ty| {
170170
match ty.sty {
171-
TyAdt(&AdtDef { ref did, .. }, substs) if self.needs_translation(*did) => {
171+
TyKind::Adt(&AdtDef { ref did, .. }, substs) if self.needs_translation(*did) => {
172172
// we fold bottom-up, so the code above is invalid, as it assumes the
173173
// substs (that have been folded already) are yet untranslated
174174
if let Some(target_def_id) = (self.translate_orig)(self.id_mapping, *did) {
@@ -178,13 +178,13 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
178178
ty
179179
}
180180
},
181-
TyRef(region, ty, mutbl) => {
181+
TyKind::Ref(region, ty, mutbl) => {
182182
let ty_and_mut = TypeAndMut { ty, mutbl };
183183
self.tcx.mk_ref(self.translate_region(region), ty_and_mut)
184184
},
185-
TyFnDef(did, substs) => {
185+
TyKind::FnDef(did, substs) => {
186186
// TODO: this might be buggy as *technically* the substs are
187-
// already translated (see TyAdt for a possible fix)
187+
// already translated (see TyKind::Adt for a possible fix)
188188
if let Some((target_def_id, target_substs)) =
189189
self.translate_orig_substs(index_map, did, substs)
190190
{
@@ -193,7 +193,7 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
193193
ty
194194
}
195195
},
196-
TyDynamic(preds, region) => {
196+
TyKind::Dynamic(preds, region) => {
197197
// hacky error catching mechanism
198198
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
199199
use std::cell::Cell;
@@ -259,7 +259,7 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
259259
ty
260260
}
261261
},
262-
TyProjection(proj) => {
262+
TyKind::Projection(proj) => {
263263
if let Some((target_def_id, target_substs)) =
264264
self.translate_orig_substs(index_map,
265265
proj.item_def_id,
@@ -269,16 +269,16 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
269269
ty
270270
}
271271
},
272-
TyAnon(did, substs) => {
272+
TyKind::Opaque(did, substs) => {
273273
if let Some((target_def_id, target_substs)) =
274274
self.translate_orig_substs(index_map, did, substs)
275275
{
276-
self.tcx.mk_anon(target_def_id, target_substs)
276+
self.tcx.mk_opaque(target_def_id, target_substs)
277277
} else {
278278
ty
279279
}
280280
},
281-
TyParam(param) => {
281+
TyKind::Param(param) => {
282282
// FIXME: we should check `has_self` if this gets used again!
283283
if param.idx != 0 && self.translate_params { // `Self` is special
284284
let orig_def_id = index_map[&param.idx];
@@ -518,15 +518,15 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for InferenceCleanupFolder<'a, 'gcx,
518518

519519
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
520520
use rustc::ty::TypeAndMut;
521-
use rustc::ty::TypeVariants::{TyError, TyInfer, TyRef};
521+
use rustc::ty::TyKind;
522522

523523
let t1 = ty.super_fold_with(self);
524524
match t1.sty {
525-
TyRef(region, ty, mutbl) if region.needs_infer() => {
525+
TyKind::Ref(region, ty, mutbl) if region.needs_infer() => {
526526
let ty_and_mut = TypeAndMut { ty, mutbl };
527527
self.infcx.tcx.mk_ref(self.infcx.tcx.types.re_erased, ty_and_mut)
528528
},
529-
TyInfer(_) => self.infcx.tcx.mk_ty(TyError),
529+
TyKind::Infer(_) => self.infcx.tcx.mk_ty(TyKind::Error),
530530
_ => t1,
531531
}
532532
}

src/semcheck/traverse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ fn diff_traits(changes: &mut ChangeSet,
498498
output: bool) {
499499
use rustc::hir::Unsafety::Unsafe;
500500
use rustc::ty::subst::UnpackedKind::Type;
501-
use rustc::ty::{ParamTy, Predicate, TyS, TypeVariants::*};
501+
use rustc::ty::{ParamTy, Predicate, TyS, TyKind};
502502

503503
debug!("diff_traits: old: {:?}, new: {:?}, output: {:?}", old, new, output);
504504

@@ -525,7 +525,7 @@ fn diff_traits(changes: &mut ChangeSet,
525525

526526
if id_mapping.is_private_trait(&trait_ref.def_id) &&
527527
trait_ref.substs.len() == 1 {
528-
if let Type(&TyS { sty: TyParam(ParamTy { idx: 0, ..}), ..}) =
528+
if let Type(&TyS { sty: TyKind::Param(ParamTy { idx: 0, ..}), ..}) =
529529
trait_ref.substs[0].unpack() {
530530
old_sealed = true;
531531
}

0 commit comments

Comments
 (0)