]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / libsyntax / parse / obsolete.rs
1 // Copyright 2012-2014 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 //! Support for parsing unsupported, old syntaxes, for the purpose of reporting errors. Parsing of
12 //! these syntaxes is tested by compile-test/obsolete-syntax.rs.
13 //!
14 //! Obsolete syntax that becomes too hard to parse can be removed.
15
16 use ast::{Expr, ExprTup};
17 use codemap::Span;
18 use parse::parser;
19 use parse::token;
20 use ptr::P;
21
22 /// The specific types of unsupported syntax
23 #[derive(Copy, PartialEq, Eq, Hash)]
24 pub enum ObsoleteSyntax {
25     Sized,
26     ForSized,
27     ProcType,
28     ProcExpr,
29     ClosureType,
30     ClosureKind,
31 }
32
33 pub trait ParserObsoleteMethods {
34     /// Reports an obsolete syntax non-fatal error.
35     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
36     /// Reports an obsolete syntax non-fatal error, and returns
37     /// a placeholder expression
38     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>;
39     fn report(&mut self,
40               sp: Span,
41               kind: ObsoleteSyntax,
42               kind_str: &str,
43               desc: &str);
44     fn is_obsolete_ident(&mut self, ident: &str) -> bool;
45     fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
46 }
47
48 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
49     /// Reports an obsolete syntax non-fatal error.
50     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
51         let (kind_str, desc) = match kind {
52             ObsoleteSyntax::ForSized => (
53                 "for Sized?",
54                 "no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
55                  by default",
56             ),
57             ObsoleteSyntax::ProcType => (
58                 "the `proc` type",
59                 "use unboxed closures instead",
60             ),
61             ObsoleteSyntax::ProcExpr => (
62                 "`proc` expression",
63                 "use a `move ||` expression instead",
64             ),
65             ObsoleteSyntax::ClosureType => (
66                 "`|usize| -> bool` closure type",
67                 "use unboxed closures instead, no type annotation needed"
68             ),
69             ObsoleteSyntax::ClosureKind => (
70                 "`:`, `&mut:`, or `&:`",
71                 "rely on inference instead"
72             ),
73             ObsoleteSyntax::Sized => (
74                 "`Sized? T` for removing the `Sized` bound",
75                 "write `T: ?Sized` instead"
76             ),
77         };
78
79         self.report(sp, kind, kind_str, desc);
80     }
81
82     /// Reports an obsolete syntax non-fatal error, and returns
83     /// a placeholder expression
84     fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr> {
85         self.obsolete(sp, kind);
86         self.mk_expr(sp.lo, sp.hi, ExprTup(vec![]))
87     }
88
89     fn report(&mut self,
90               sp: Span,
91               kind: ObsoleteSyntax,
92               kind_str: &str,
93               desc: &str) {
94         self.span_err(sp,
95                       &format!("obsolete syntax: {}", kind_str)[]);
96
97         if !self.obsolete_set.contains(&kind) {
98             self.sess
99                 .span_diagnostic
100                 .handler()
101                 .note(&format!("{}", desc)[]);
102             self.obsolete_set.insert(kind);
103         }
104     }
105
106     fn is_obsolete_ident(&mut self, ident: &str) -> bool {
107         match self.token {
108             token::Ident(sid, _) => {
109                 token::get_ident(sid) == ident
110             }
111             _ => false
112         }
113     }
114
115     fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
116         if self.is_obsolete_ident(ident) {
117             self.bump();
118             true
119         } else {
120             false
121         }
122     }
123 }