Skip to content

Commit 64cd30e

Browse files
committed
Declare &foo[] to be obsolete syntax. Modify the obsolete mechanism to
support warnings.
1 parent dfc5c0f commit 64cd30e

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/libsyntax/parse/obsolete.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum ObsoleteSyntax {
2828
ProcExpr,
2929
ClosureType,
3030
ClosureKind,
31+
EmptyIndex,
3132
}
3233

3334
pub trait ParserObsoleteMethods {
@@ -48,35 +49,46 @@ pub trait ParserObsoleteMethods {
4849
impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
4950
/// Reports an obsolete syntax non-fatal error.
5051
fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax) {
51-
let (kind_str, desc) = match kind {
52+
let (kind_str, desc, error) = match kind {
5253
ObsoleteSyntax::ForSized => (
5354
"for Sized?",
5455
"no longer required. Traits (and their `Self` type) do not have the `Sized` bound \
5556
by default",
57+
true,
5658
),
5759
ObsoleteSyntax::ProcType => (
5860
"the `proc` type",
5961
"use unboxed closures instead",
62+
true,
6063
),
6164
ObsoleteSyntax::ProcExpr => (
6265
"`proc` expression",
6366
"use a `move ||` expression instead",
67+
true,
6468
),
6569
ObsoleteSyntax::ClosureType => (
6670
"`|usize| -> bool` closure type",
6771
"use unboxed closures instead, no type annotation needed"
72+
true,
6873
),
6974
ObsoleteSyntax::ClosureKind => (
7075
"`:`, `&mut:`, or `&:`",
7176
"rely on inference instead"
77+
true,
7278
),
7379
ObsoleteSyntax::Sized => (
7480
"`Sized? T` for removing the `Sized` bound",
7581
"write `T: ?Sized` instead"
82+
true,
83+
),
84+
ObsoleteSyntax::EmptyIndex => (
85+
"[]",
86+
"write `[..]` instead",
87+
false, // warning for now
7688
),
7789
};
7890

79-
self.report(sp, kind, kind_str, desc);
91+
self.report(sp, kind, kind_str, desc, error);
8092
}
8193

8294
/// Reports an obsolete syntax non-fatal error, and returns
@@ -90,9 +102,13 @@ impl<'a> ParserObsoleteMethods for parser::Parser<'a> {
90102
sp: Span,
91103
kind: ObsoleteSyntax,
92104
kind_str: &str,
93-
desc: &str) {
94-
self.span_err(sp,
95-
&format!("obsolete syntax: {}", kind_str)[]);
105+
desc: &str,
106+
error: bool) {
107+
if error {
108+
self.span_err(sp, &format!("obsolete syntax: {}", kind_str)[]);
109+
} else {
110+
self.span_warn(sp, &format!("obsolete syntax: {}", kind_str)[]);
111+
}
96112

97113
if !self.obsolete_set.contains(&kind) {
98114
self.sess

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,8 +2552,9 @@ impl<'a> Parser<'a> {
25522552
parameters: ast::PathParameters::none(),
25532553
}
25542554
}).collect();
2555+
let span = mk_sp(lo, hi);
25552556
let path = ast::Path {
2556-
span: mk_sp(lo, hi),
2557+
span: span,
25572558
global: true,
25582559
segments: segments,
25592560
};
@@ -2562,10 +2563,8 @@ impl<'a> Parser<'a> {
25622563
let ix = self.mk_expr(bracket_pos, hi, range);
25632564
let index = self.mk_index(e, ix);
25642565
e = self.mk_expr(lo, hi, index);
2565-
// Enable after snapshot.
2566-
// self.span_warn(e.span, "deprecated slicing syntax: `[]`");
2567-
// self.span_note(e.span,
2568-
// "use `&expr[..]` to construct a slice of the whole of expr");
2566+
2567+
self.obsolete(span, ObsoleteSyntax::EmptyIndex);
25692568
} else {
25702569
let ix = self.parse_expr();
25712570
hi = self.span.hi;

src/test/compile-fail/slice-1.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
// except according to those terms.
1010

1111
// Test slicing &expr[] is deprecated and gives a helpful error message.
12-
//
13-
// ignore-test
1412

1513
struct Foo;
1614

1715
fn main() {
1816
let x = Foo;
19-
&x[]; //~ WARNING deprecated slicing syntax: `[]`
20-
//~^ NOTE use `&expr[..]` to construct a slice of the whole of expr
21-
//~^^ ERROR cannot index a value of type `Foo`
17+
&x[];
18+
//~^ WARN obsolete syntax
19+
//~| ERROR cannot index
2220
}

0 commit comments

Comments
 (0)