|
1 | 1 | //! Used by `rustc` when compiling a plugin crate. |
2 | 2 |
|
3 | 3 | use rustc_hir as hir; |
4 | | -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; |
| 4 | +use rustc_hir::def_id::LocalDefId; |
5 | 5 | use rustc_hir::itemlikevisit::ItemLikeVisitor; |
6 | 6 | use rustc_middle::ty::query::Providers; |
7 | 7 | use rustc_middle::ty::TyCtxt; |
@@ -31,33 +31,25 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for RegistrarFinder<'tcx> { |
31 | 31 | } |
32 | 32 |
|
33 | 33 | /// Finds the function marked with `#[plugin_registrar]`, if any. |
34 | | -pub fn find_plugin_registrar(tcx: TyCtxt<'_>) -> Option<DefId> { |
35 | | - tcx.plugin_registrar_fn(LOCAL_CRATE) |
36 | | -} |
37 | | - |
38 | | -fn plugin_registrar_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<DefId> { |
39 | | - assert_eq!(cnum, LOCAL_CRATE); |
40 | | - |
| 34 | +fn plugin_registrar_fn(tcx: TyCtxt<'_>, (): ()) -> Option<LocalDefId> { |
41 | 35 | let mut finder = RegistrarFinder { tcx, registrars: Vec::new() }; |
42 | 36 | tcx.hir().krate().visit_all_item_likes(&mut finder); |
43 | 37 |
|
44 | | - match finder.registrars.len() { |
45 | | - 0 => None, |
46 | | - 1 => { |
47 | | - let (def_id, _) = finder.registrars.pop().unwrap(); |
48 | | - Some(def_id.to_def_id()) |
49 | | - } |
50 | | - _ => { |
51 | | - let diagnostic = tcx.sess.diagnostic(); |
52 | | - let mut e = diagnostic.struct_err("multiple plugin registration functions found"); |
53 | | - for &(_, span) in &finder.registrars { |
54 | | - e.span_note(span, "one is here"); |
55 | | - } |
56 | | - e.emit(); |
57 | | - diagnostic.abort_if_errors(); |
58 | | - unreachable!(); |
| 38 | + let (def_id, span) = finder.registrars.pop()?; |
| 39 | + |
| 40 | + if !finder.registrars.is_empty() { |
| 41 | + let diagnostic = tcx.sess.diagnostic(); |
| 42 | + let mut e = diagnostic.struct_err("multiple plugin registration functions found"); |
| 43 | + e.span_note(span, "one is here"); |
| 44 | + for &(_, span) in &finder.registrars { |
| 45 | + e.span_note(span, "one is here"); |
59 | 46 | } |
| 47 | + e.emit(); |
| 48 | + diagnostic.abort_if_errors(); |
| 49 | + unreachable!(); |
60 | 50 | } |
| 51 | + |
| 52 | + Some(def_id) |
61 | 53 | } |
62 | 54 |
|
63 | 55 | pub fn provide(providers: &mut Providers) { |
|
0 commit comments