11//! Registering limits:
2- //! * recursion_limit,
3- //! * move_size_limit, and
4- //! * type_length_limit
2+ //! - recursion_limit: there are various parts of the compiler that must impose arbitrary limits
3+ //! on how deeply they recurse to prevent stack overflow.
4+ //! - move_size_limit
5+ //! - type_length_limit
6+ //! - pattern_complexity_limit
57//!
6- //! There are various parts of the compiler that must impose arbitrary limits
7- //! on how deeply they recurse to prevent stack overflow. Users can override
8- //! this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
9- //! just peeks and looks for that attribute.
8+ //! Users can override these limits via an attribute on the crate like
9+ //! `#![recursion_limit="22"]`. This pass just looks for those attributes.
1010
1111use std:: num:: IntErrorKind ;
1212
1313use rustc_ast:: attr:: AttributeExt ;
14+ use rustc_middle:: bug;
15+ use rustc_middle:: query:: Providers ;
1416use rustc_session:: { Limit , Limits , Session } ;
1517use rustc_span:: { Symbol , sym} ;
1618
17- use crate :: error:: LimitInvalid ;
18- use crate :: query:: Providers ;
19+ use crate :: errors:: LimitInvalid ;
1920
20- pub fn provide ( providers : & mut Providers ) {
21+ pub ( crate ) fn provide ( providers : & mut Providers ) {
2122 providers. limits = |tcx, ( ) | Limits {
2223 recursion_limit : get_recursion_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess ) ,
2324 move_size_limit : get_limit (
2425 tcx. hir ( ) . krate_attrs ( ) ,
2526 tcx. sess ,
2627 sym:: move_size_limit,
27- tcx. sess . opts . unstable_opts . move_size_limit . unwrap_or ( 0 ) ,
28+ Limit :: new ( tcx. sess . opts . unstable_opts . move_size_limit . unwrap_or ( 0 ) ) ,
2829 ) ,
2930 type_length_limit : get_limit (
3031 tcx. hir ( ) . krate_attrs ( ) ,
3132 tcx. sess ,
3233 sym:: type_length_limit,
33- 2usize . pow ( 24 ) ,
34+ Limit :: new ( 2usize . pow ( 24 ) ) ,
35+ ) ,
36+ pattern_complexity_limit : get_limit (
37+ tcx. hir ( ) . krate_attrs ( ) ,
38+ tcx. sess ,
39+ sym:: pattern_complexity_limit,
40+ Limit :: unlimited ( ) ,
3441 ) ,
3542 }
3643}
3744
38- pub fn get_recursion_limit ( krate_attrs : & [ impl AttributeExt ] , sess : & Session ) -> Limit {
39- get_limit ( krate_attrs, sess, sym:: recursion_limit, 128 )
45+ // This one is separate because it must be read prior to macro expansion.
46+ pub ( crate ) fn get_recursion_limit ( krate_attrs : & [ impl AttributeExt ] , sess : & Session ) -> Limit {
47+ get_limit ( krate_attrs, sess, sym:: recursion_limit, Limit :: new ( 128 ) )
4048}
4149
4250fn get_limit (
4351 krate_attrs : & [ impl AttributeExt ] ,
4452 sess : & Session ,
4553 name : Symbol ,
46- default : usize ,
54+ default : Limit ,
4755) -> Limit {
48- match get_limit_size ( krate_attrs, sess, name) {
49- Some ( size) => Limit :: new ( size) ,
50- None => Limit :: new ( default) ,
51- }
52- }
53-
54- pub fn get_limit_size (
55- krate_attrs : & [ impl AttributeExt ] ,
56- sess : & Session ,
57- name : Symbol ,
58- ) -> Option < usize > {
5956 for attr in krate_attrs {
6057 if !attr. has_name ( name) {
6158 continue ;
6259 }
6360
6461 if let Some ( sym) = attr. value_str ( ) {
6562 match sym. as_str ( ) . parse ( ) {
66- Ok ( n) => return Some ( n) ,
63+ Ok ( n) => return Limit :: new ( n) ,
6764 Err ( e) => {
6865 let error_str = match e. kind ( ) {
6966 IntErrorKind :: PosOverflow => "`limit` is too large" ,
@@ -84,5 +81,5 @@ pub fn get_limit_size(
8481 }
8582 }
8683 }
87- None
84+ default
8885}
0 commit comments