]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/parse/obsolete.rs
Auto merge of #30641 - tsion:match-range, r=eddyb
[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 codemap::Span;
17 use parse::parser;
18
19 /// The specific types of unsupported syntax
20 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
21 pub enum ObsoleteSyntax {
22     ClosureKind,
23     ExternCrateString,
24 }
25
26 pub trait ParserObsoleteMethods {
27     /// Reports an obsolete syntax non-fatal error.
28     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
29     fn report(&mut self,
30               sp: Span,
31               kind: ObsoleteSyntax,
32               kind_str: &str,
33               desc: &str,
34               error: bool);
35 }
36
37 impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
38     /// Reports an obsolete syntax non-fatal error.
39     fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
40         let (kind_str, desc, error) = match kind {
41             ObsoleteSyntax::ClosureKind => (
42                 "`:`, `&mut:`, or `&:`",
43                 "rely on inference instead",
44                 true,
45             ),
46             ObsoleteSyntax::ExternCrateString => (
47                 "\"crate-name\"",
48                 "use an identifier not in quotes instead",
49                 false, // warning for now
50             ),
51         };
52
53         self.report(sp, kind, kind_str, desc, error);
54     }
55
56     fn report(&mut self,
57               sp: Span,
58               kind: ObsoleteSyntax,
59               kind_str: &str,
60               desc: &str,
61               error: bool) {
62         let mut err = if error {
63             self.diagnostic().struct_span_err(sp, &format!("obsolete syntax: {}", kind_str))
64         } else {
65             self.diagnostic().struct_span_warn(sp, &format!("obsolete syntax: {}", kind_str))
66         };
67
68         if !self.obsolete_set.contains(&kind) &&
69             (error || self.sess.span_diagnostic.can_emit_warnings) {
70             err.note(&format!("{}", desc));
71             self.obsolete_set.insert(kind);
72         }
73         err.emit();
74     }
75 }