Skip to content

Commit 97534c5

Browse files
committed
Switched to the new rustc driver introduced in nightly.
1 parent 5a66eb4 commit 97534c5

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

Diff for: src/bin/rust_semverver.rs

+61-51
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#![feature(rustc_private)]
2+
#![feature(result_map_or_else)]
23

34
extern crate getopts;
45
extern crate rustc;
56
extern crate rustc_codegen_utils;
67
extern crate rustc_driver;
78
extern crate rustc_errors;
9+
extern crate rustc_interface;
810
extern crate rustc_metadata;
911
extern crate syntax;
1012

1113
use log::debug;
1214
use rustc::{hir::def_id::*, middle::cstore::ExternCrate};
13-
use rustc_driver::{driver::CompileController, Compilation};
15+
use rustc_driver::Callbacks;
16+
use rustc_interface::interface;
1417
use semverver::run_analysis;
15-
use std::convert::TryInto;
1618
use std::{
1719
path::Path,
1820
process::{exit, Command},
@@ -33,7 +35,7 @@ fn main() {
3335

3436
debug!("running rust-semverver compiler driver");
3537
exit(
36-
rustc_driver::run(move || {
38+
{
3739
use std::env;
3840

3941
if std::env::args().any(|a| a == "--version" || a == "-V") {
@@ -86,59 +88,67 @@ fn main() {
8688
.collect()
8789
};
8890

89-
let verbose = std::env::var("RUST_SEMVER_VERBOSE") == Ok("true".to_string());
90-
let api_guidelines = std::env::var("RUST_SEMVER_API_GUIDELINES") == Ok("true".to_string());
91-
let version = if let Ok(ver) = std::env::var("RUST_SEMVER_CRATE_VERSION") {
92-
ver
93-
} else {
94-
"no_version".to_owned()
95-
};
96-
97-
let mut controller = CompileController::basic();
98-
99-
controller.after_analysis.callback = Box::new(move |state| {
100-
debug!("running rust-semverver after_analysis...");
101-
let tcx = state.tcx.unwrap();
102-
103-
// To select the old and new crates we look at the position of the declaration in the
104-
// source file. The first one will be the `old` and the other will be `new`. This is
105-
// unfortunately a bit hacky... See issue #64 for details.
106-
107-
let mut crates: Vec<_> = tcx
108-
.crates()
109-
.iter()
110-
.flat_map(|crate_num| {
111-
let def_id = DefId {
112-
krate: *crate_num,
113-
index: CRATE_DEF_INDEX,
114-
};
115-
116-
match *tcx.extern_crate(def_id) {
117-
Some(ExternCrate {
118-
span, direct: true, ..
119-
}) if span.data().lo.to_usize() > 0 => Some((span.data().lo.to_usize(), def_id)),
120-
_ => None,
91+
struct SemverCallbacks;
92+
93+
impl Callbacks for SemverCallbacks {
94+
fn after_analysis(&mut self, compiler: &interface::Compiler) -> bool {
95+
debug!("running rust-semverver after_analysis callback");
96+
97+
let verbose =
98+
std::env::var("RUST_SEMVER_VERBOSE") == Ok("true".to_string());
99+
let api_guidelines =
100+
std::env::var("RUST_SEMVER_API_GUIDELINES") == Ok("true".to_string());
101+
let version = if let Ok(ver) = std::env::var("RUST_SEMVER_CRATE_VERSION") {
102+
ver
103+
} else {
104+
"no_version".to_owned()
105+
};
106+
107+
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
108+
// To select the old and new crates we look at the position of the
109+
// declaration in the source file. The first one will be the `old`
110+
// and the other will be `new`. This is unfortunately a bit hacky...
111+
// See issue #64 for details.
112+
113+
let mut crates: Vec<_> = tcx
114+
.crates()
115+
.iter()
116+
.flat_map(|crate_num| {
117+
let def_id = DefId {
118+
krate: *crate_num,
119+
index: CRATE_DEF_INDEX,
120+
};
121+
122+
match *tcx.extern_crate(def_id) {
123+
Some(ExternCrate {
124+
span, direct: true, ..
125+
}) if span.data().lo.to_usize() > 0 =>
126+
Some((span.data().lo.to_usize(), def_id)),
127+
_ => None,
128+
}
129+
})
130+
.collect();
131+
132+
crates.sort_by_key(|&(span_lo, _)| span_lo);
133+
134+
if let [(_, old_def_id), (_, new_def_id)] = *crates.as_slice() {
135+
debug!("running semver analysis");
136+
let changes = run_analysis(tcx, old_def_id, new_def_id);
137+
changes.output(tcx.sess, &version, verbose, api_guidelines);
138+
} else {
139+
tcx.sess.err("could not find crate old and new crates");
121140
}
122-
})
123-
.collect();
141+
});
124142

125-
crates.sort_by_key(|&(span_lo, _)| span_lo);
143+
debug!("rust-semverver after_analysis callback finished!");
126144

127-
if let [(_, old_def_id), (_, new_def_id)] = *crates.as_slice() {
128-
debug!("running semver analysis");
129-
let changes = run_analysis(tcx, old_def_id, new_def_id);
130-
changes.output(tcx.sess, &version, verbose, api_guidelines);
131-
} else {
132-
tcx.sess.err("could not find crate old and new crates");
145+
false
133146
}
134-
135-
debug!("running rust-semverver after_analysis finished!");
136-
});
137-
controller.after_analysis.stop = Compilation::Stop;
147+
}
138148

139149
let args = args;
140-
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
141-
}).try_into()
142-
.expect("exit code too large"),
150+
rustc_driver::run_compiler(&args, &mut SemverCallbacks, None, None)
151+
}
152+
.map_or_else(|_| 1, |_| 0),
143153
)
144154
}

Diff for: src/mapping.rs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl IdMapping {
143143
match param.kind {
144144
GenericParamDefKind::Lifetime => unreachable!(),
145145
GenericParamDefKind::Type { .. } => (),
146+
GenericParamDefKind::Const => unreachable!(),
146147
};
147148

148149
self.type_params.insert(param.def_id, param.clone());

Diff for: src/translate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ impl<'a, 'gcx, 'tcx> TranslationContext<'a, 'gcx, 'tcx> {
149149
success.set(false);
150150
self.tcx.mk_param_from_def(def)
151151
}
152-
}
152+
},
153+
GenericParamDefKind::Const => unreachable!(),
153154
});
154155

155156
if success.get() {

Diff for: src/traverse.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use rustc::{
2424
},
2525
ty::{
2626
subst::{InternalSubsts, Subst},
27-
AssociatedItem, GenericParamDef, GenericParamDefKind, Generics, Ty, TyCtxt, Visibility,
28-
Visibility::Public,
27+
AssociatedItem, DefIdTree, GenericParamDef, GenericParamDefKind, Generics, Ty, TyCtxt,
28+
Visibility, Visibility::Public,
2929
},
3030
};
3131
use std::collections::{BTreeMap, HashSet, VecDeque};
@@ -1036,7 +1036,7 @@ fn diff_trait_impls<'a, 'tcx>(
10361036
let old_trait_def_id = tcx.impl_trait_ref(*old_impl_def_id).unwrap().def_id;
10371037

10381038
let old_impl_parent_def = tcx
1039-
.parent_def_id(*old_impl_def_id)
1039+
.parent(*old_impl_def_id)
10401040
.and_then(|did| tcx.describe_def(did));
10411041
let old_impl_parent_is_fn = match old_impl_parent_def {
10421042
Some(Def::Fn(_)) | Some(Def::Method(_)) => true,
@@ -1050,7 +1050,7 @@ fn diff_trait_impls<'a, 'tcx>(
10501050
if !match_trait_impl(tcx, &to_new, *old_impl_def_id) {
10511051
changes.new_change_impl(
10521052
*old_impl_def_id,
1053-
tcx.item_path_str(*old_impl_def_id),
1053+
tcx.def_path_str(*old_impl_def_id),
10541054
tcx.def_span(*old_impl_def_id),
10551055
);
10561056
changes.add_change(ChangeType::TraitImplTightened, *old_impl_def_id, None);
@@ -1064,7 +1064,7 @@ fn diff_trait_impls<'a, 'tcx>(
10641064
let new_trait_def_id = tcx.impl_trait_ref(*new_impl_def_id).unwrap().def_id;
10651065

10661066
let new_impl_parent_def = tcx
1067-
.parent_def_id(*new_impl_def_id)
1067+
.parent(*new_impl_def_id)
10681068
.and_then(|did| tcx.describe_def(did));
10691069
let new_impl_parent_is_fn = match new_impl_parent_def {
10701070
Some(Def::Fn(_)) | Some(Def::Method(_)) => true,
@@ -1078,7 +1078,7 @@ fn diff_trait_impls<'a, 'tcx>(
10781078
if !match_trait_impl(tcx, &to_old, *new_impl_def_id) {
10791079
changes.new_change_impl(
10801080
*new_impl_def_id,
1081-
tcx.item_path_str(*new_impl_def_id),
1081+
tcx.def_path_str(*new_impl_def_id),
10821082
tcx.def_span(*new_impl_def_id),
10831083
);
10841084
changes.add_change(ChangeType::TraitImplLoosened, *new_impl_def_id, None);

Diff for: src/typeck.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ impl<'a, 'gcx, 'tcx> TypeComparisonContext<'a, 'gcx, 'tcx> {
195195
} else {
196196
self.infcx.tcx.mk_param_from_def(def)
197197
}
198-
}
198+
},
199+
GenericParamDefKind::Const => unreachable!(),
199200
})
200201
}
201202

0 commit comments

Comments
 (0)