]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/lib.rs
add a preliminary existential test; not really enough
[rust.git] / src / libsyntax / lib.rs
1 //! The Rust parser and macro expander.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
8        test(attr(deny(warnings))))]
9
10 #![deny(rust_2018_idioms)]
11 #![deny(internal)]
12 #![deny(unused_lifetimes)]
13
14 #![feature(bind_by_move_pattern_guards)]
15 #![feature(const_fn)]
16 #![feature(const_transmute)]
17 #![feature(crate_visibility_modifier)]
18 #![feature(label_break_value)]
19 #![feature(nll)]
20 #![feature(rustc_attrs)]
21 #![feature(rustc_diagnostic_macros)]
22 #![feature(step_trait)]
23 #![feature(try_trait)]
24 #![feature(unicode_internals)]
25
26 #![recursion_limit="256"]
27
28 #[allow(unused_extern_crates)]
29 extern crate serialize as rustc_serialize; // used by deriving
30
31 pub use errors;
32 use rustc_data_structures::sync::Lock;
33 use rustc_data_structures::bit_set::GrowableBitSet;
34 pub use rustc_data_structures::thin_vec::ThinVec;
35 use ast::AttrId;
36 use syntax_pos::edition::Edition;
37
38 const MACRO_ARGUMENTS: Option<&'static str> = Some("macro arguments");
39
40 // A variant of 'try!' that panics on an Err. This is used as a crutch on the
41 // way towards a non-panic!-prone parser. It should be used for fatal parsing
42 // errors; eventually we plan to convert all code using panictry to just use
43 // normal try.
44 macro_rules! panictry {
45     ($e:expr) => ({
46         use std::result::Result::{Ok, Err};
47         use errors::FatalError;
48         match $e {
49             Ok(e) => e,
50             Err(mut e) => {
51                 e.emit();
52                 FatalError.raise()
53             }
54         }
55     })
56 }
57
58 // A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
59 macro_rules! panictry_buffer {
60     ($handler:expr, $e:expr) => ({
61         use std::result::Result::{Ok, Err};
62         use errors::{FatalError, DiagnosticBuilder};
63         match $e {
64             Ok(e) => e,
65             Err(errs) => {
66                 for e in errs {
67                     DiagnosticBuilder::new_diagnostic($handler, e).emit();
68                 }
69                 FatalError.raise()
70             }
71         }
72     })
73 }
74
75 #[macro_export]
76 macro_rules! unwrap_or {
77     ($opt:expr, $default:expr) => {
78         match $opt {
79             Some(x) => x,
80             None => $default,
81         }
82     }
83 }
84
85 pub struct Globals {
86     used_attrs: Lock<GrowableBitSet<AttrId>>,
87     known_attrs: Lock<GrowableBitSet<AttrId>>,
88     syntax_pos_globals: syntax_pos::Globals,
89 }
90
91 impl Globals {
92     fn new(edition: Edition) -> Globals {
93         Globals {
94             // We have no idea how many attributes their will be, so just
95             // initiate the vectors with 0 bits. We'll grow them as necessary.
96             used_attrs: Lock::new(GrowableBitSet::new_empty()),
97             known_attrs: Lock::new(GrowableBitSet::new_empty()),
98             syntax_pos_globals: syntax_pos::Globals::new(edition),
99         }
100     }
101 }
102
103 pub fn with_globals<F, R>(edition: Edition, f: F) -> R
104     where F: FnOnce() -> R
105 {
106     let globals = Globals::new(edition);
107     GLOBALS.set(&globals, || {
108         syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
109     })
110 }
111
112 pub fn with_default_globals<F, R>(f: F) -> R
113     where F: FnOnce() -> R
114 {
115     with_globals(edition::DEFAULT_EDITION, f)
116 }
117
118 scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
119
120 #[macro_use]
121 pub mod diagnostics {
122     #[macro_use]
123     pub mod macros;
124     pub mod plugin;
125     pub mod metadata;
126 }
127
128 // N.B., this module needs to be declared first so diagnostics are
129 // registered before they are used.
130 pub mod error_codes;
131
132 pub mod util {
133     pub mod lev_distance;
134     pub mod node_count;
135     pub mod parser;
136     #[cfg(test)]
137     pub mod parser_testing;
138     pub mod map_in_place;
139 }
140
141 pub mod json;
142
143 pub mod ast;
144 pub mod attr;
145 pub mod source_map;
146 #[macro_use]
147 pub mod config;
148 pub mod entry;
149 pub mod feature_gate;
150 pub mod mut_visit;
151 pub mod parse;
152 pub mod ptr;
153 pub mod show_span;
154 pub mod std_inject;
155 pub use syntax_pos::edition;
156 pub use syntax_pos::symbol;
157 pub mod test;
158 pub mod tokenstream;
159 pub mod visit;
160
161 pub mod print {
162     pub mod pp;
163     pub mod pprust;
164 }
165
166 pub mod ext {
167     pub use syntax_pos::hygiene;
168     pub mod base;
169     pub mod build;
170     pub mod derive;
171     pub mod expand;
172     pub mod placeholders;
173     pub mod source_util;
174
175     pub mod tt {
176         pub mod transcribe;
177         pub mod macro_parser;
178         pub mod macro_rules;
179         pub mod quoted;
180     }
181 }
182
183 pub mod early_buffered_lints;
184
185 #[cfg(test)]
186 mod test_snippet;
187
188 __build_diagnostic_array! { libsyntax, DIAGNOSTICS }