22
33use syntax:: ast;
44use syntax:: attr;
5- use errors;
65use syntax_pos:: Span ;
7- use rustc:: hir:: map:: Map ;
86use rustc:: hir:: itemlikevisit:: ItemLikeVisitor ;
97use rustc:: hir;
8+ use rustc:: hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
9+ use rustc:: ty:: TyCtxt ;
10+ use rustc:: ty:: query:: Providers ;
1011
1112struct RegistrarFinder {
1213 registrars : Vec < ( ast:: NodeId , Span ) > ,
@@ -30,21 +31,27 @@ impl<'v> ItemLikeVisitor<'v> for RegistrarFinder {
3031}
3132
3233/// Find the function marked with `#[plugin_registrar]`, if any.
33- pub fn find_plugin_registrar ( diagnostic : & errors:: Handler ,
34- hir_map : & Map )
35- -> Option < ast:: NodeId > {
36- let krate = hir_map. krate ( ) ;
34+ pub fn find_plugin_registrar < ' tcx > ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > ) -> Option < DefId > {
35+ tcx. plugin_registrar_fn ( LOCAL_CRATE )
36+ }
37+
38+ fn plugin_registrar_fn < ' tcx > (
39+ tcx : TyCtxt < ' _ , ' tcx , ' tcx > ,
40+ cnum : CrateNum ,
41+ ) -> Option < DefId > {
42+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
3743
3844 let mut finder = RegistrarFinder { registrars : Vec :: new ( ) } ;
39- krate. visit_all_item_likes ( & mut finder) ;
45+ tcx . hir ( ) . krate ( ) . visit_all_item_likes ( & mut finder) ;
4046
4147 match finder. registrars . len ( ) {
4248 0 => None ,
4349 1 => {
4450 let ( node_id, _) = finder. registrars . pop ( ) . unwrap ( ) ;
45- Some ( node_id)
51+ Some ( tcx . hir ( ) . local_def_id ( node_id) )
4652 } ,
4753 _ => {
54+ let diagnostic = tcx. sess . diagnostic ( ) ;
4855 let mut e = diagnostic. struct_err ( "multiple plugin registration functions found" ) ;
4956 for & ( _, span) in & finder. registrars {
5057 e. span_note ( span, "one is here" ) ;
@@ -55,3 +62,11 @@ pub fn find_plugin_registrar(diagnostic: &errors::Handler,
5562 }
5663 }
5764}
65+
66+
67+ pub fn provide ( providers : & mut Providers < ' _ > ) {
68+ * providers = Providers {
69+ plugin_registrar_fn,
70+ ..* providers
71+ } ;
72+ }
0 commit comments