1
1
//! 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
5
7
//!
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.
10
10
11
11
use std:: num:: IntErrorKind ;
12
12
13
13
use rustc_ast:: attr:: AttributeExt ;
14
+ use rustc_middle:: bug;
15
+ use rustc_middle:: query:: Providers ;
14
16
use rustc_session:: { Limit , Limits , Session } ;
15
17
use rustc_span:: { Symbol , sym} ;
16
18
17
- use crate :: error:: LimitInvalid ;
18
- use crate :: query:: Providers ;
19
+ use crate :: errors:: LimitInvalid ;
19
20
20
- pub fn provide ( providers : & mut Providers ) {
21
+ pub ( crate ) fn provide ( providers : & mut Providers ) {
21
22
providers. limits = |tcx, ( ) | Limits {
22
23
recursion_limit : get_recursion_limit ( tcx. hir ( ) . krate_attrs ( ) , tcx. sess ) ,
23
24
move_size_limit : get_limit (
24
25
tcx. hir ( ) . krate_attrs ( ) ,
25
26
tcx. sess ,
26
27
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 ) ) ,
28
29
) ,
29
30
type_length_limit : get_limit (
30
31
tcx. hir ( ) . krate_attrs ( ) ,
31
32
tcx. sess ,
32
33
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 ( ) ,
34
41
) ,
35
42
}
36
43
}
37
44
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 ) )
40
48
}
41
49
42
50
fn get_limit (
43
51
krate_attrs : & [ impl AttributeExt ] ,
44
52
sess : & Session ,
45
53
name : Symbol ,
46
- default : usize ,
54
+ default : Limit ,
47
55
) -> 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 > {
59
56
for attr in krate_attrs {
60
57
if !attr. has_name ( name) {
61
58
continue ;
62
59
}
63
60
64
61
if let Some ( sym) = attr. value_str ( ) {
65
62
match sym. as_str ( ) . parse ( ) {
66
- Ok ( n) => return Some ( n) ,
63
+ Ok ( n) => return Limit :: new ( n) ,
67
64
Err ( e) => {
68
65
let error_str = match e. kind ( ) {
69
66
IntErrorKind :: PosOverflow => "`limit` is too large" ,
@@ -84,5 +81,5 @@ pub fn get_limit_size(
84
81
}
85
82
}
86
83
}
87
- None
84
+ default
88
85
}
0 commit comments