2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-08 15:50:29 -07:00
|
|
|
/*!
|
|
|
|
Support for parsing unsupported, old syntaxes, for the
|
|
|
|
purpose of reporting errors. Parsing of these syntaxes
|
|
|
|
is tested by compile-test/obsolete-syntax.rs.
|
|
|
|
|
|
|
|
Obsolete syntax that becomes too hard to parse can be
|
|
|
|
removed.
|
|
|
|
*/
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
use ast::{Expr, ExprLit, LitNil};
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::{Span, respan};
|
2013-01-08 19:37:25 -08:00
|
|
|
use parse::parser::Parser;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::token;
|
|
|
|
|
2012-09-08 15:50:29 -07:00
|
|
|
/// The specific types of unsupported syntax
|
2014-02-23 21:29:35 +11:00
|
|
|
#[deriving(Eq, Hash)]
|
2012-09-08 15:50:29 -07:00
|
|
|
pub enum ObsoleteSyntax {
|
2013-05-06 00:43:19 -04:00
|
|
|
ObsoleteSwap,
|
2013-01-28 10:46:43 -08:00
|
|
|
ObsoleteUnsafeBlock,
|
2013-03-07 15:44:21 -08:00
|
|
|
ObsoleteBareFnType,
|
2013-06-04 21:43:41 -07:00
|
|
|
ObsoleteMultipleLocalDecl,
|
2013-08-02 14:30:00 -07:00
|
|
|
ObsoleteUnsafeExternFn,
|
2013-08-09 01:25:24 -07:00
|
|
|
ObsoleteTraitFuncVisibility,
|
2013-08-02 21:41:06 -07:00
|
|
|
ObsoleteConstPointer,
|
2013-10-01 14:41:59 -07:00
|
|
|
ObsoleteLoopAsContinue,
|
2013-11-01 18:02:30 -07:00
|
|
|
ObsoleteEnumWildcard,
|
2013-11-07 19:25:39 -08:00
|
|
|
ObsoleteStructWildcard,
|
2013-11-18 18:25:25 -08:00
|
|
|
ObsoleteVecDotDotWildcard,
|
|
|
|
ObsoleteBoxedClosure,
|
2013-11-19 18:15:10 -08:00
|
|
|
ObsoleteClosureType,
|
2013-12-10 21:50:38 -08:00
|
|
|
ObsoleteMultipleImport,
|
2014-01-03 14:15:08 -08:00
|
|
|
ObsoleteManagedPattern,
|
2014-01-31 18:25:08 -08:00
|
|
|
ObsoleteManagedString,
|
2014-02-01 23:18:45 +11:00
|
|
|
ObsoleteManagedVec,
|
2012-09-19 18:00:26 -07:00
|
|
|
}
|
2012-09-08 15:50:29 -07:00
|
|
|
|
2013-06-14 18:21:47 -07:00
|
|
|
pub trait ParserObsoleteMethods {
|
|
|
|
/// Reports an obsolete syntax non-fatal error.
|
2013-12-30 14:04:00 -08:00
|
|
|
fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax);
|
2013-06-14 18:21:47 -07:00
|
|
|
// Reports an obsolete syntax non-fatal error, and returns
|
|
|
|
// a placeholder expression
|
2013-12-30 14:04:00 -08:00
|
|
|
fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr;
|
|
|
|
fn report(&mut self,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2013-06-14 18:21:47 -07:00
|
|
|
kind: ObsoleteSyntax,
|
|
|
|
kind_str: &str,
|
|
|
|
desc: &str);
|
2013-12-30 14:04:00 -08:00
|
|
|
fn is_obsolete_ident(&mut self, ident: &str) -> bool;
|
|
|
|
fn eat_obsolete_ident(&mut self, ident: &str) -> bool;
|
2013-06-14 18:21:47 -07:00
|
|
|
}
|
|
|
|
|
2014-03-09 16:54:34 +02:00
|
|
|
impl<'a> ParserObsoleteMethods for Parser<'a> {
|
2012-09-08 15:50:29 -07:00
|
|
|
/// Reports an obsolete syntax non-fatal error.
|
2013-12-30 14:04:00 -08:00
|
|
|
fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
|
2012-09-08 15:50:29 -07:00
|
|
|
let (kind_str, desc) = match kind {
|
2013-05-06 00:43:19 -04:00
|
|
|
ObsoleteSwap => (
|
|
|
|
"swap",
|
2014-02-01 04:35:36 +08:00
|
|
|
"use std::mem::{swap, replace} instead"
|
2013-05-06 00:43:19 -04:00
|
|
|
),
|
2013-01-23 11:43:58 -08:00
|
|
|
ObsoleteUnsafeBlock => (
|
|
|
|
"non-standalone unsafe block",
|
|
|
|
"use an inner `unsafe { ... }` block instead"
|
2013-01-28 10:46:43 -08:00
|
|
|
),
|
2013-03-07 15:44:21 -08:00
|
|
|
ObsoleteBareFnType => (
|
|
|
|
"bare function type",
|
2013-11-19 12:21:21 -08:00
|
|
|
"use `|A| -> B` or `extern fn(A) -> B` instead"
|
2013-03-07 15:44:21 -08:00
|
|
|
),
|
2013-06-04 21:43:41 -07:00
|
|
|
ObsoleteMultipleLocalDecl => (
|
|
|
|
"declaration of multiple locals at once",
|
|
|
|
"instead of e.g. `let a = 1, b = 2`, write \
|
|
|
|
`let (a, b) = (1, 2)`."
|
|
|
|
),
|
2013-08-02 14:30:00 -07:00
|
|
|
ObsoleteUnsafeExternFn => (
|
|
|
|
"unsafe external function",
|
|
|
|
"external functions are always unsafe; remove the `unsafe` \
|
|
|
|
keyword"
|
|
|
|
),
|
2013-08-09 01:25:24 -07:00
|
|
|
ObsoleteTraitFuncVisibility => (
|
|
|
|
"visibility not necessary",
|
|
|
|
"trait functions inherit the visibility of the trait itself"
|
|
|
|
),
|
2013-08-02 21:41:06 -07:00
|
|
|
ObsoleteConstPointer => (
|
|
|
|
"const pointer",
|
|
|
|
"instead of `&const Foo` or `@const Foo`, write `&Foo` or \
|
|
|
|
`@Foo`"
|
|
|
|
),
|
2013-10-01 14:41:59 -07:00
|
|
|
ObsoleteLoopAsContinue => (
|
|
|
|
"`loop` instead of `continue`",
|
|
|
|
"`loop` is now only used for loops and `continue` is used for \
|
|
|
|
skipping iterations"
|
|
|
|
),
|
2013-11-01 18:02:30 -07:00
|
|
|
ObsoleteEnumWildcard => (
|
|
|
|
"enum wildcard",
|
|
|
|
"use `..` instead of `*` for matching all enum fields"
|
|
|
|
),
|
|
|
|
ObsoleteStructWildcard => (
|
|
|
|
"struct wildcard",
|
|
|
|
"use `..` instead of `_` for matching trailing struct fields"
|
|
|
|
),
|
2013-11-07 19:25:39 -08:00
|
|
|
ObsoleteVecDotDotWildcard => (
|
|
|
|
"vec slice wildcard",
|
|
|
|
"use `..` instead of `.._` for matching slices"
|
|
|
|
),
|
2013-11-18 18:25:25 -08:00
|
|
|
ObsoleteBoxedClosure => (
|
|
|
|
"managed or owned closure",
|
|
|
|
"managed closures have been removed and owned closures are \
|
|
|
|
now written `proc()`"
|
|
|
|
),
|
2013-11-19 18:15:10 -08:00
|
|
|
ObsoleteClosureType => (
|
|
|
|
"closure type",
|
|
|
|
"closures are now written `|A| -> B` rather than `&fn(A) -> \
|
|
|
|
B`."
|
|
|
|
),
|
2013-12-10 21:50:38 -08:00
|
|
|
ObsoleteMultipleImport => (
|
|
|
|
"multiple imports",
|
|
|
|
"only one import is allowed per `use` statement"
|
|
|
|
),
|
2014-01-03 14:15:08 -08:00
|
|
|
ObsoleteManagedPattern => (
|
|
|
|
"managed pointer pattern",
|
|
|
|
"use a nested `match` expression instead of a managed box \
|
|
|
|
pattern"
|
|
|
|
),
|
2014-01-31 18:25:08 -08:00
|
|
|
ObsoleteManagedString => (
|
|
|
|
"managed string",
|
|
|
|
"use `Rc<~str>` instead of a managed string"
|
|
|
|
),
|
2014-02-01 23:18:45 +11:00
|
|
|
ObsoleteManagedVec => (
|
|
|
|
"managed vector",
|
|
|
|
"use `Rc<~[T]>` instead of a managed vector"
|
|
|
|
),
|
2012-09-08 15:50:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
self.report(sp, kind, kind_str, desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reports an obsolete syntax non-fatal error, and returns
|
|
|
|
// a placeholder expression
|
2013-12-30 14:04:00 -08:00
|
|
|
fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> @Expr {
|
2012-09-08 15:50:29 -07:00
|
|
|
self.obsolete(sp, kind);
|
2014-01-09 15:05:33 +02:00
|
|
|
self.mk_expr(sp.lo, sp.hi, ExprLit(@respan(sp, LitNil)))
|
2012-09-08 15:50:29 -07:00
|
|
|
}
|
|
|
|
|
2013-12-30 14:04:00 -08:00
|
|
|
fn report(&mut self,
|
2013-08-31 18:13:04 +02:00
|
|
|
sp: Span,
|
2013-05-31 15:17:22 -07:00
|
|
|
kind: ObsoleteSyntax,
|
|
|
|
kind_str: &str,
|
|
|
|
desc: &str) {
|
2013-09-27 21:01:58 -07:00
|
|
|
self.span_err(sp, format!("obsolete syntax: {}", kind_str));
|
2012-09-08 15:50:29 -07:00
|
|
|
|
2013-03-21 15:41:37 -04:00
|
|
|
if !self.obsolete_set.contains(&kind) {
|
2013-09-27 21:01:58 -07:00
|
|
|
self.sess.span_diagnostic.handler().note(format!("{}", desc));
|
2013-03-21 15:41:37 -04:00
|
|
|
self.obsolete_set.insert(kind);
|
2012-09-08 15:50:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 15:09:41 -08:00
|
|
|
fn is_obsolete_ident(&mut self, ident: &str) -> bool {
|
|
|
|
match self.token {
|
2013-05-12 00:25:31 -04:00
|
|
|
token::IDENT(sid, _) => {
|
2014-02-14 07:07:09 +02:00
|
|
|
token::get_ident(sid).equiv(&ident)
|
2012-09-08 15:50:29 -07:00
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 14:04:00 -08:00
|
|
|
fn eat_obsolete_ident(&mut self, ident: &str) -> bool {
|
2012-09-08 15:50:29 -07:00
|
|
|
if self.is_obsolete_ident(ident) {
|
|
|
|
self.bump();
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|