1010//! just peeks and looks for that attribute.
1111
1212use crate :: bug;
13- use rustc_ast as ast;
14- use rustc_data_structures:: sync:: OnceCell ;
13+ use crate :: ty;
14+ use rustc_ast:: Attribute ;
15+ use rustc_session:: Limit ;
1516use rustc_session:: Session ;
1617use rustc_span:: symbol:: { sym, Symbol } ;
1718
1819use std:: num:: IntErrorKind ;
1920
20- pub fn update_limits ( sess : & Session , krate : & ast:: Crate ) {
21- update_limit ( sess, krate, & sess. recursion_limit , sym:: recursion_limit, 128 ) ;
22- update_limit ( sess, krate, & sess. move_size_limit , sym:: move_size_limit, 0 ) ;
23- update_limit ( sess, krate, & sess. type_length_limit , sym:: type_length_limit, 1048576 ) ;
24- update_limit ( sess, krate, & sess. const_eval_limit , sym:: const_eval_limit, 1_000_000 ) ;
21+ pub fn provide ( providers : & mut ty:: query:: Providers ) {
22+ providers. recursion_limit = |tcx, ( ) | get_recursion_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess ) ;
23+ providers. move_size_limit =
24+ |tcx, ( ) | get_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess , sym:: move_size_limit, 0 ) . 0 ;
25+ providers. type_length_limit =
26+ |tcx, ( ) | get_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess , sym:: type_length_limit, 1048576 ) ;
27+ providers. const_eval_limit =
28+ |tcx, ( ) | get_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess , sym:: const_eval_limit, 1_000_000 ) ;
2529}
2630
27- fn update_limit (
28- sess : & Session ,
29- krate : & ast:: Crate ,
30- limit : & OnceCell < impl From < usize > + std:: fmt:: Debug > ,
31- name : Symbol ,
32- default : usize ,
33- ) {
34- for attr in & krate. attrs {
31+ pub fn get_recursion_limit ( krate_attrs : & [ Attribute ] , sess : & Session ) -> Limit {
32+ get_limit ( krate_attrs, sess, sym:: recursion_limit, 128 )
33+ }
34+
35+ fn get_limit ( krate_attrs : & [ Attribute ] , sess : & Session , name : Symbol , default : usize ) -> Limit {
36+ for attr in krate_attrs {
3537 if !sess. check_name ( attr, name) {
3638 continue ;
3739 }
3840
3941 if let Some ( s) = attr. value_str ( ) {
4042 match s. as_str ( ) . parse ( ) {
41- Ok ( n) => {
42- limit. set ( From :: from ( n) ) . unwrap ( ) ;
43- return ;
44- }
43+ Ok ( n) => return Limit :: new ( n) ,
4544 Err ( e) => {
4645 let mut err =
4746 sess. struct_span_err ( attr. span , "`limit` must be a non-negative integer" ) ;
@@ -68,5 +67,5 @@ fn update_limit(
6867 }
6968 }
7069 }
71- limit . set ( From :: from ( default) ) . unwrap ( ) ;
70+ return Limit :: new ( default) ;
7271}
0 commit comments