]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
auto merge of #11956 : edwardw/rust/issue-7556, r=cmr
[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 use std::to_bytes;
26
27 /// The specific types of unsupported syntax
28 #[deriving(Eq)]
29 pub enum ObsoleteSyntax {
30     ObsoleteSwap,
31     ObsoleteUnsafeBlock,
32     ObsoleteBareFnType,
33     ObsoleteNamedExternModule,
34     ObsoleteMultipleLocalDecl,
35     ObsoleteUnsafeExternFn,
36     ObsoleteTraitFuncVisibility,
37     ObsoleteConstPointer,
38     ObsoleteLoopAsContinue,
39     ObsoleteEnumWildcard,
40     ObsoleteStructWildcard,
41     ObsoleteVecDotDotWildcard,
42     ObsoleteBoxedClosure,
43     ObsoleteClosureType,
44     ObsoleteMultipleImport,
45     ObsoleteExternModAttributesInParens,
46     ObsoleteManagedPattern,
47     ObsoleteManagedString,
48     ObsoleteManagedVec,
49 }
50
51 impl to_bytes::IterBytes for ObsoleteSyntax {
52     #[inline]
53     fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
54         (*self as uint).iter_bytes(lsb0, f)
55     }
56 }
57
58 pub trait ParserObsoleteMethods {
59     /// Reports an obsolete syntax non-fatal error.
60     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
61     // Reports an obsolete syntax non-fatal error, and returns
62     // a placeholder expression
63     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
64     fn report(&mut self,
65               sp: Span,
66               kind: ObsoleteSyntax,
67               kind_str: &str,
68               desc: &str);
69     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
70     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
71 }
72
73 impl ParserObsoleteMethods for Parser {
74     /// Reports an obsolete syntax non-fatal error.
75     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
76         let (kind_str, desc) = match kind {
77             ObsoleteSwap => (
78                 "swap",
79                 "use std::mem::{swap, replace} instead"
80             ),
81             ObsoleteUnsafeBlock => (
82                 "non-standalone unsafe block",
83                 "use an inner `unsafe { ... }` block instead"
84             ),
85             ObsoleteBareFnType => (
86                 "bare function type",
87                 "use `|A| -> B` or `extern fn(A) -> B` instead"
88             ),
89             ObsoleteNamedExternModule => (
90                 "named external module",
91                 "instead of `extern mod foo { ... }`, write `mod foo { \
92                  extern { ... } }`"
93             ),
94             ObsoleteMultipleLocalDecl => (
95                 "declaration of multiple locals at once",
96                 "instead of e.g. `let a = 1, b = 2`, write \
97                  `let (a, b) = (1, 2)`."
98             ),
99             ObsoleteUnsafeExternFn => (
100                 "unsafe external function",
101                 "external functions are always unsafe; remove the `unsafe` \
102                  keyword"
103             ),
104             ObsoleteTraitFuncVisibility => (
105                 "visibility not necessary",
106                 "trait functions inherit the visibility of the trait itself"
107             ),
108             ObsoleteConstPointer => (
109                 "const pointer",
110                 "instead of `&const Foo` or `@const Foo`, write `&Foo` or \
111                  `@Foo`"
112             ),
113             ObsoleteLoopAsContinue => (
114                 "`loop` instead of `continue`",
115                 "`loop` is now only used for loops and `continue` is used for \
116                  skipping iterations"
117             ),
118             ObsoleteEnumWildcard => (
119                 "enum wildcard",
120                 "use `..` instead of `*` for matching all enum fields"
121             ),
122             ObsoleteStructWildcard => (
123                 "struct wildcard",
124                 "use `..` instead of `_` for matching trailing struct fields"
125             ),
126             ObsoleteVecDotDotWildcard => (
127                 "vec slice wildcard",
128                 "use `..` instead of `.._` for matching slices"
129             ),
130             ObsoleteBoxedClosure => (
131                 "managed or owned closure",
132                 "managed closures have been removed and owned closures are \
133                  now written `proc()`"
134             ),
135             ObsoleteClosureType => (
136                 "closure type",
137                 "closures are now written `|A| -> B` rather than `&fn(A) -> \
138                  B`."
139             ),
140             ObsoleteMultipleImport => (
141                 "multiple imports",
142                 "only one import is allowed per `use` statement"
143             ),
144             ObsoleteExternModAttributesInParens => (
145                 "`extern mod` with linkage attribute list",
146                 "use `extern mod foo = \"bar\";` instead of \
147                 `extern mod foo (name = \"bar\")`"
148             ),
149             ObsoleteManagedPattern => (
150                 "managed pointer pattern",
151                 "use a nested `match` expression instead of a managed box \
152                  pattern"
153             ),
154             ObsoleteManagedString => (
155                 "managed string",
156                 "use `Rc<~str>` instead of a managed string"
157             ),
158             ObsoleteManagedVec => (
159                 "managed vector",
160                 "use `Rc<~[T]>` instead of a managed vector"
161             ),
162         };
163
164         self.report(sp, kind, kind_str, desc);
165     }
166
167     // Reports an obsolete syntax non-fatal error, and returns
168     // a placeholder expression
169     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
170         self.obsolete(sp, kind);
171         self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
172     }
173
174     fn report(&mut self,
175               sp: Span,
176               kind: ObsoleteSyntax,
177               kind_str: &str,
178               desc: &str) {
179         self.span_err(sp, format!("obsolete syntax: {}", kind_str));
180
181         if !self.obsolete_set.contains(&kind) {
182             self.sess.span_diagnostic.handler().note(format!("{}", desc));
183             self.obsolete_set.insert(kind);
184         }
185     }
186
187     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
188         match self.token {
189             token::IDENT(sid, _) => {
190                 let interned_string = token::get_ident(sid.name);
191                 interned_string.equiv(&ident)
192             }
193             _ => false
194         }
195     }
196
197     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
198         if self.is_obsolete_ident(ident) {
199             self.bump();
200             true
201         } else {
202             false
203         }
204     }
205 }