Skip to content

Commit 31263f3

Browse files
committed
Auto merge of #52285 - ljedrz:dyn_librustc_driver, r=nikomatsakis
Deny bare trait objects in librustc_driver Enforce `#![deny(bare_trait_objects)]` in `src/librustc_driver`.
2 parents 55c04ba + db7170c commit 31263f3

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

src/librustc_driver/driver.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn spawn_thread_pool<F: FnOnce(config::Options) -> R + sync::Send, R: sync::
100100
// the thread local rustc uses. syntax_globals and syntax_pos_globals are
101101
// captured and set on the new threads. ty::tls::with_thread_locals sets up
102102
// thread local callbacks from libsyntax
103-
let main_handler = move |worker: &mut FnMut()| {
103+
let main_handler = move |worker: &mut dyn FnMut()| {
104104
syntax::GLOBALS.set(syntax_globals, || {
105105
syntax_pos::GLOBALS.set(syntax_pos_globals, || {
106106
ty::tls::with_thread_locals(|| {
@@ -118,7 +118,7 @@ pub fn spawn_thread_pool<F: FnOnce(config::Options) -> R + sync::Send, R: sync::
118118
}
119119

120120
pub fn compile_input(
121-
codegen_backend: Box<CodegenBackend>,
121+
codegen_backend: Box<dyn CodegenBackend>,
122122
sess: &Session,
123123
cstore: &CStore,
124124
input_path: &Option<PathBuf>,
@@ -399,10 +399,10 @@ pub struct CompileController<'a> {
399399

400400
/// Allows overriding default rustc query providers,
401401
/// after `default_provide` has installed them.
402-
pub provide: Box<Fn(&mut ty::query::Providers) + 'a>,
402+
pub provide: Box<dyn Fn(&mut ty::query::Providers) + 'a>,
403403
/// Same as `provide`, but only for non-local crates,
404404
/// applied after `default_provide_extern`.
405-
pub provide_extern: Box<Fn(&mut ty::query::Providers) + 'a>,
405+
pub provide_extern: Box<dyn Fn(&mut ty::query::Providers) + 'a>,
406406
}
407407

408408
impl<'a> CompileController<'a> {
@@ -471,10 +471,10 @@ impl<'a> ::CompilerCalls<'a> for CompileController<'a> {
471471
}
472472
fn late_callback(
473473
&mut self,
474-
codegen_backend: &::CodegenBackend,
474+
codegen_backend: &dyn (::CodegenBackend),
475475
matches: &::getopts::Matches,
476476
sess: &Session,
477-
cstore: &::CrateStore,
477+
cstore: &dyn (::CrateStore),
478478
input: &Input,
479479
odir: &Option<PathBuf>,
480480
ofile: &Option<PathBuf>,
@@ -496,7 +496,7 @@ pub struct PhaseController<'a> {
496496
// If true then the compiler will try to run the callback even if the phase
497497
// ends with an error. Note that this is not always possible.
498498
pub run_callback_on_error: bool,
499-
pub callback: Box<Fn(&mut CompileState) + 'a>,
499+
pub callback: Box<dyn Fn(&mut CompileState) + 'a>,
500500
}
501501

502502
impl<'a> PhaseController<'a> {
@@ -1175,7 +1175,7 @@ pub fn default_provide_extern(providers: &mut ty::query::Providers) {
11751175
/// miscellaneous analysis passes on the crate. Return various
11761176
/// structures carrying the results of the analysis.
11771177
pub fn phase_3_run_analysis_passes<'tcx, F, R>(
1178-
codegen_backend: &CodegenBackend,
1178+
codegen_backend: &dyn CodegenBackend,
11791179
control: &CompileController,
11801180
sess: &'tcx Session,
11811181
cstore: &'tcx CrateStoreDyn,
@@ -1191,7 +1191,7 @@ where
11911191
F: for<'a> FnOnce(
11921192
TyCtxt<'a, 'tcx, 'tcx>,
11931193
ty::CrateAnalysis,
1194-
mpsc::Receiver<Box<Any + Send>>,
1194+
mpsc::Receiver<Box<dyn Any + Send>>,
11951195
CompileResult,
11961196
) -> R,
11971197
{
@@ -1324,10 +1324,10 @@ where
13241324
/// Run the codegen backend, after which the AST and analysis can
13251325
/// be discarded.
13261326
pub fn phase_4_codegen<'a, 'tcx>(
1327-
codegen_backend: &CodegenBackend,
1327+
codegen_backend: &dyn CodegenBackend,
13281328
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1329-
rx: mpsc::Receiver<Box<Any + Send>>,
1330-
) -> Box<Any> {
1329+
rx: mpsc::Receiver<Box<dyn Any + Send>>,
1330+
) -> Box<dyn Any> {
13311331
time(tcx.sess, "resolving dependency formats", || {
13321332
::rustc::middle::dependency_format::calculate(tcx)
13331333
});

src/librustc_driver/lib.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
//!
1515
//! This API is completely unstable and subject to change.
1616
17+
#![deny(bare_trait_objects)]
18+
1719
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
1820
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
1921
html_root_url = "https://doc.rust-lang.org/nightly/")]
@@ -131,7 +133,7 @@ pub mod target_features {
131133
/// features is available on the target machine, by querying LLVM.
132134
pub fn add_configuration(cfg: &mut ast::CrateConfig,
133135
sess: &Session,
134-
codegen_backend: &CodegenBackend) {
136+
codegen_backend: &dyn CodegenBackend) {
135137
let tf = Symbol::intern("target_feature");
136138

137139
for feat in codegen_backend.target_features(sess) {
@@ -202,7 +204,7 @@ pub fn run<F>(run_compiler: F) -> isize
202204
0
203205
}
204206

205-
fn load_backend_from_dylib(path: &Path) -> fn() -> Box<CodegenBackend> {
207+
fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
206208
// Note that we're specifically using `open_global_now` here rather than
207209
// `open`, namely we want the behavior on Unix of RTLD_GLOBAL and RTLD_NOW,
208210
// where NOW means "bind everything right now" because we don't want
@@ -235,12 +237,12 @@ fn load_backend_from_dylib(path: &Path) -> fn() -> Box<CodegenBackend> {
235237
}
236238
}
237239

238-
pub fn get_codegen_backend(sess: &Session) -> Box<CodegenBackend> {
240+
pub fn get_codegen_backend(sess: &Session) -> Box<dyn CodegenBackend> {
239241
static INIT: Once = ONCE_INIT;
240242

241243
#[allow(deprecated)]
242244
#[no_debug]
243-
static mut LOAD: fn() -> Box<CodegenBackend> = || unreachable!();
245+
static mut LOAD: fn() -> Box<dyn CodegenBackend> = || unreachable!();
244246

245247
INIT.call_once(|| {
246248
let codegen_name = sess.opts.debugging_opts.codegen_backend.as_ref()
@@ -264,7 +266,7 @@ pub fn get_codegen_backend(sess: &Session) -> Box<CodegenBackend> {
264266
backend
265267
}
266268

267-
fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<CodegenBackend> {
269+
fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> {
268270
// For now we only allow this function to be called once as it'll dlopen a
269271
// few things, which seems to work best if we only do that once. In
270272
// general this assertion never trips due to the once guard in `get_codegen_backend`,
@@ -454,9 +456,9 @@ fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<CodegenBackend> {
454456
// See comments on CompilerCalls below for details about the callbacks argument.
455457
// The FileLoader provides a way to load files from sources other than the file system.
456458
pub fn run_compiler<'a>(args: &[String],
457-
callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
458-
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
459-
emitter_dest: Option<Box<Write + Send>>)
459+
callbacks: Box<dyn CompilerCalls<'a> + sync::Send + 'a>,
460+
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
461+
emitter_dest: Option<Box<dyn Write + Send>>)
460462
-> (CompileResult, Option<Session>)
461463
{
462464
syntax::with_globals(|| {
@@ -478,9 +480,9 @@ fn run_compiler_with_pool<'a>(
478480
matches: getopts::Matches,
479481
sopts: config::Options,
480482
cfg: ast::CrateConfig,
481-
mut callbacks: Box<CompilerCalls<'a> + sync::Send + 'a>,
482-
file_loader: Option<Box<FileLoader + Send + Sync + 'static>>,
483-
emitter_dest: Option<Box<Write + Send>>
483+
mut callbacks: Box<dyn CompilerCalls<'a> + sync::Send + 'a>,
484+
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
485+
emitter_dest: Option<Box<dyn Write + Send>>
484486
) -> (CompileResult, Option<Session>) {
485487
macro_rules! do_or_return {($expr: expr, $sess: expr) => {
486488
match $expr {
@@ -662,10 +664,10 @@ pub trait CompilerCalls<'a> {
662664
/// be called just before actual compilation starts (and before build_controller
663665
/// is called), after all arguments etc. have been completely handled.
664666
fn late_callback(&mut self,
665-
_: &CodegenBackend,
667+
_: &dyn CodegenBackend,
666668
_: &getopts::Matches,
667669
_: &Session,
668-
_: &CrateStore,
670+
_: &dyn CrateStore,
669671
_: &Input,
670672
_: &Option<PathBuf>,
671673
_: &Option<PathBuf>)
@@ -870,10 +872,10 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
870872
}
871873

872874
fn late_callback(&mut self,
873-
codegen_backend: &CodegenBackend,
875+
codegen_backend: &dyn CodegenBackend,
874876
matches: &getopts::Matches,
875877
sess: &Session,
876-
cstore: &CrateStore,
878+
cstore: &dyn CrateStore,
877879
input: &Input,
878880
odir: &Option<PathBuf>,
879881
ofile: &Option<PathBuf>)
@@ -979,7 +981,7 @@ pub fn enable_save_analysis(control: &mut CompileController) {
979981

980982
impl RustcDefaultCalls {
981983
pub fn list_metadata(sess: &Session,
982-
cstore: &CrateStore,
984+
cstore: &dyn CrateStore,
983985
matches: &getopts::Matches,
984986
input: &Input)
985987
-> Compilation {
@@ -1007,7 +1009,7 @@ impl RustcDefaultCalls {
10071009
}
10081010

10091011

1010-
fn print_crate_info(codegen_backend: &CodegenBackend,
1012+
fn print_crate_info(codegen_backend: &dyn CodegenBackend,
10111013
sess: &Session,
10121014
input: Option<&Input>,
10131015
odir: &Option<PathBuf>,
@@ -1486,7 +1488,7 @@ fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<as
14861488
/// Runs `f` in a suitable thread for running `rustc`; returns a
14871489
/// `Result` with either the return value of `f` or -- if a panic
14881490
/// occurs -- the panic value.
1489-
pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
1491+
pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<dyn Any + Send>>
14901492
where F: FnOnce() -> R + Send + 'static,
14911493
R: Send + 'static,
14921494
{

src/librustc_driver/pretty.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl PpSourceMode {
170170
hir_map: Option<&hir_map::Map<'tcx>>,
171171
f: F)
172172
-> A
173-
where F: FnOnce(&PrinterSupport) -> A
173+
where F: FnOnce(&dyn PrinterSupport) -> A
174174
{
175175
match *self {
176176
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
@@ -208,7 +208,7 @@ impl PpSourceMode {
208208
id: &str,
209209
f: F)
210210
-> A
211-
where F: FnOnce(&HirPrinterSupport, &hir::Crate) -> A
211+
where F: FnOnce(&dyn HirPrinterSupport, &hir::Crate) -> A
212212
{
213213
match *self {
214214
PpmNormal => {
@@ -265,7 +265,7 @@ trait PrinterSupport: pprust::PpAnn {
265265
///
266266
/// (Rust does not yet support upcasting from a trait object to
267267
/// an object for one of its super-traits.)
268-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn;
268+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn;
269269
}
270270

271271
trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
@@ -281,7 +281,7 @@ trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
281281
///
282282
/// (Rust does not yet support upcasting from a trait object to
283283
/// an object for one of its super-traits.)
284-
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn;
284+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn;
285285

286286
/// Computes an user-readable representation of a path, if possible.
287287
fn node_path(&self, id: ast::NodeId) -> Option<String> {
@@ -305,7 +305,7 @@ impl<'hir> PrinterSupport for NoAnn<'hir> {
305305
self.sess
306306
}
307307

308-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
308+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
309309
self
310310
}
311311
}
@@ -319,7 +319,7 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
319319
self.hir_map.as_ref()
320320
}
321321

322-
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
322+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
323323
self
324324
}
325325
}
@@ -346,7 +346,7 @@ impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
346346
self.sess
347347
}
348348

349-
fn pp_ann<'a>(&'a self) -> &'a pprust::PpAnn {
349+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
350350
self
351351
}
352352
}
@@ -397,7 +397,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
397397
self.hir_map.as_ref()
398398
}
399399

400-
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
400+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
401401
self
402402
}
403403
}
@@ -458,7 +458,7 @@ impl<'a> PrinterSupport for HygieneAnnotation<'a> {
458458
self.sess
459459
}
460460

461-
fn pp_ann(&self) -> &pprust::PpAnn {
461+
fn pp_ann(&self) -> &dyn pprust::PpAnn {
462462
self
463463
}
464464
}
@@ -496,7 +496,7 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
496496
Some(&self.tcx.hir)
497497
}
498498

499-
fn pp_ann<'a>(&'a self) -> &'a pprust_hir::PpAnn {
499+
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
500500
self
501501
}
502502

@@ -896,7 +896,7 @@ pub fn print_after_parsing(sess: &Session,
896896

897897
if let PpmSource(s) = ppm {
898898
// Silently ignores an identified node.
899-
let out: &mut Write = &mut out;
899+
let out: &mut dyn Write = &mut out;
900900
s.call_with_pp_support(sess, None, move |annotation| {
901901
debug!("pretty printing source code {:?}", s);
902902
let sess = annotation.sess();
@@ -953,7 +953,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
953953
match (ppm, opt_uii) {
954954
(PpmSource(s), _) => {
955955
// Silently ignores an identified node.
956-
let out: &mut Write = &mut out;
956+
let out: &mut dyn Write = &mut out;
957957
s.call_with_pp_support(sess, Some(hir_map), move |annotation| {
958958
debug!("pretty printing source code {:?}", s);
959959
let sess = annotation.sess();
@@ -969,7 +969,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
969969
}
970970

971971
(PpmHir(s), None) => {
972-
let out: &mut Write = &mut out;
972+
let out: &mut dyn Write = &mut out;
973973
s.call_with_pp_support_hir(sess,
974974
cstore,
975975
hir_map,
@@ -993,7 +993,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
993993
}
994994

995995
(PpmHirTree(s), None) => {
996-
let out: &mut Write = &mut out;
996+
let out: &mut dyn Write = &mut out;
997997
s.call_with_pp_support_hir(sess,
998998
cstore,
999999
hir_map,
@@ -1009,7 +1009,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10091009
}
10101010

10111011
(PpmHir(s), Some(uii)) => {
1012-
let out: &mut Write = &mut out;
1012+
let out: &mut dyn Write = &mut out;
10131013
s.call_with_pp_support_hir(sess,
10141014
cstore,
10151015
hir_map,
@@ -1043,7 +1043,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10431043
}
10441044

10451045
(PpmHirTree(s), Some(uii)) => {
1046-
let out: &mut Write = &mut out;
1046+
let out: &mut dyn Write = &mut out;
10471047
s.call_with_pp_support_hir(sess,
10481048
cstore,
10491049
hir_map,
@@ -1137,7 +1137,7 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
11371137
Some(code) => {
11381138
let variants = gather_flowgraph_variants(tcx.sess);
11391139

1140-
let out: &mut Write = &mut out;
1140+
let out: &mut dyn Write = &mut out;
11411141

11421142
print_flowgraph(variants, tcx, code, mode, out)
11431143
}

src/librustc_driver/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ impl Emitter for ExpectErrorEmitter {
8888
}
8989
}
9090

91-
fn errors(msgs: &[&str]) -> (Box<Emitter + sync::Send>, usize) {
91+
fn errors(msgs: &[&str]) -> (Box<dyn Emitter + sync::Send>, usize) {
9292
let v = msgs.iter().map(|m| m.to_string()).collect();
93-
(box ExpectErrorEmitter { messages: v } as Box<Emitter + sync::Send>, msgs.len())
93+
(box ExpectErrorEmitter { messages: v } as Box<dyn Emitter + sync::Send>, msgs.len())
9494
}
9595

9696
fn test_env<F>(source_string: &str,
97-
args: (Box<Emitter + sync::Send>, usize),
97+
args: (Box<dyn Emitter + sync::Send>, usize),
9898
body: F)
9999
where F: FnOnce(Env)
100100
{
@@ -112,7 +112,7 @@ fn test_env<F>(source_string: &str,
112112
fn test_env_with_pool<F>(
113113
options: config::Options,
114114
source_string: &str,
115-
(emitter, expected_err_count): (Box<Emitter + sync::Send>, usize),
115+
(emitter, expected_err_count): (Box<dyn Emitter + sync::Send>, usize),
116116
body: F
117117
)
118118
where F: FnOnce(Env)

0 commit comments

Comments
 (0)