]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
auto merge of #13049 : alexcrichton/rust/io-fill, r=huonw
[rust.git] / src / libsyntax / parse / obsolete.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12 Support for parsing unsupported, old syntaxes, for the
13 purpose of reporting errors. Parsing of these syntaxes
14 is tested by compile-test/obsolete-syntax.rs.
15
16 Obsolete syntax that becomes too hard to parse can be
17 removed.
18 */
19
20 use ast::{Expr, ExprLit, LitNil};
21 use codemap::{Span, respan};
22 use parse::parser::Parser;
23 use parse::token;
24
25 /// The specific types of unsupported syntax
26 #[deriving(Eq, TotalEq, Hash)]
27 pub enum ObsoleteSyntax {
28     ObsoleteSwap,
29     ObsoleteUnsafeBlock,
30     ObsoleteBareFnType,
31     ObsoleteMultipleLocalDecl,
32     ObsoleteUnsafeExternFn,
33     ObsoleteTraitFuncVisibility,
34     ObsoleteConstPointer,
35     ObsoleteLoopAsContinue,
36     ObsoleteEnumWildcard,
37     ObsoleteStructWildcard,
38     ObsoleteVecDotDotWildcard,
39     ObsoleteBoxedClosure,
40     ObsoleteClosureType,
41     ObsoleteMultipleImport,
42     ObsoleteManagedPattern,
43     ObsoleteManagedString,
44     ObsoleteManagedVec,
45 }
46
47 pub trait ParserObsoleteMethods {
48     /// Reports an obsolete syntax non-fatal error.
49     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
50     // Reports an obsolete syntax non-fatal error, and returns
51     // a placeholder expression
52     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
53     fn report(&mut self,
54               sp: Span,
55               kind: ObsoleteSyntax,
56               kind_str: &str,
57               desc: &str);
58     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
59     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
60 }
61
62 impl<'a> ParserObsoleteMethods for Parser<'a> {
63     /// Reports an obsolete syntax non-fatal error.
64     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
65         let (kind_str, desc) = match kind {
66             ObsoleteSwap => (
67                 "swap",
68                 "use std::mem::{swap, replace} instead"
69             ),
70             ObsoleteUnsafeBlock => (
71                 "non-standalone unsafe block",
72                 "use an inner `unsafe { ... }` block instead"
73             ),
74             ObsoleteBareFnType => (
75                 "bare function type",
76                 "use `|A| -> B` or `extern fn(A) -> B` instead"
77             ),
78             ObsoleteMultipleLocalDecl => (
79                 "declaration of multiple locals at once",
80                 "instead of e.g. `let a = 1, b = 2`, write \
81                  `let (a, b) = (1, 2)`."
82             ),
83             ObsoleteUnsafeExternFn => (
84                 "unsafe external function",
85                 "external functions are always unsafe; remove the `unsafe` \
86                  keyword"
87             ),
88             ObsoleteTraitFuncVisibility => (
89                 "visibility not necessary",
90                 "trait functions inherit the visibility of the trait itself"
91             ),
92             ObsoleteConstPointer => (
93                 "const pointer",
94                 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
95                  `@Foo`"
96             ),
97             ObsoleteLoopAsContinue => (
98                 "`loop` instead of `continue`",
99                 "`loop` is now only used for loops and `continue` is used for \
100                  skipping iterations"
101             ),
102             ObsoleteEnumWildcard => (
103                 "enum wildcard",
104                 "use `..` instead of `*` for matching all enum fields"
105             ),
106             ObsoleteStructWildcard => (
107                 "struct wildcard",
108                 "use `..` instead of `_` for matching trailing struct fields"
109             ),
110             ObsoleteVecDotDotWildcard => (
111                 "vec slice wildcard",
112                 "use `..` instead of `.._` for matching slices"
113             ),
114             ObsoleteBoxedClosure => (
115                 "managed or owned closure",
116                 "managed closures have been removed and owned closures are \
117                  now written `proc()`"
118             ),
119             ObsoleteClosureType => (
120                 "closure type",
121                 "closures are now written `|A| -> B` rather than `&fn(A) -> \
122                  B`."
123             ),
124             ObsoleteMultipleImport => (
125                 "multiple imports",
126                 "only one import is allowed per `use` statement"
127             ),
128             ObsoleteManagedPattern => (
129                 "managed pointer pattern",
130                 "use a nested `match` expression instead of a managed box \
131                  pattern"
132             ),
133             ObsoleteManagedString => (
134                 "managed string",
135                 "use `Rc<~str>` instead of a managed string"
136             ),
137             ObsoleteManagedVec => (
138                 "managed vector",
139                 "use `Rc<~[T]>` instead of a managed vector"
140             ),
141         };
142
143         self.report(sp, kind, kind_str, desc);
144     }
145
146     // Reports an obsolete syntax non-fatal error, and returns
147     // a placeholder expression
148     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
149         self.obsolete(sp, kind);
150         self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
151     }
152
153     fn report(&mut self,
154               sp: Span,
155               kind: ObsoleteSyntax,
156               kind_str: &str,
157               desc: &str) {
158         self.span_err(sp, format!("obsolete syntax: {}", kind_str));
159
160         if !self.obsolete_set.contains(&kind) {
161             self.sess.span_diagnostic.handler().note(format!("{}", desc));
162             self.obsolete_set.insert(kind);
163         }
164     }
165
166     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
167         match self.token {
168             token::IDENT(sid, _) => {
169                 token::get_ident(sid).equiv(&ident)
170             }
171             _ => false
172         }
173     }
174
175     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
176         if self.is_obsolete_ident(ident) {
177             self.bump();
178             true
179         } else {
180             false
181         }
182     }
183 }