Skip to content

Commit eb42ea8

Browse files
committed
Give type ascription a future compatibility report
1 parent ce36e88 commit eb42ea8

File tree

14 files changed

+105
-117
lines changed

14 files changed

+105
-117
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rustc_ast::{PatKind, RangeEnd, VariantData};
55
use rustc_errors::{struct_span_err, Applicability, StashKey};
66
use rustc_feature::Features;
77
use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
8-
use rustc_session::parse::{feature_err, feature_warn};
8+
use rustc_session::lint::builtin::TYPE_ASCRIPTION_SYNTAX;
9+
use rustc_session::parse::{feature_err, feature_force_warn, feature_warn};
910
use rustc_session::Session;
1011
use rustc_span::source_map::Spanned;
1112
use rustc_span::symbol::sym;
@@ -640,7 +641,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
640641
self.sess
641642
.parse_sess
642643
.span_diagnostic
643-
.steal_diagnostic(e.span, StashKey::EarlySyntaxWarning)
644+
.steal_diagnostic(e.span, StashKey::TypeAscriptionSyntax)
644645
.map(|err| err.cancel());
645646
}
646647
}
@@ -816,7 +817,16 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
816817
gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
817818
gate_all!(try_blocks, "`try` blocks are unstable");
818819
gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
819-
gate_all!(type_ascription, "type ascription is experimental");
820+
821+
// Type ascription has its own lint to participate in future-compatibility reports.
822+
// Only create these lints if no other errors have been emitted to avoid overload.
823+
if sess.parse_sess.span_diagnostic.err_count() == 0 {
824+
if let Some(spans) = spans.get(&sym::type_ascription) {
825+
for span in spans {
826+
feature_force_warn(&visitor.sess.parse_sess, *span, TYPE_ASCRIPTION_SYNTAX);
827+
}
828+
}
829+
}
820830

821831
visit::walk_crate(&mut visitor, krate);
822832
}

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ pub enum StashKey {
460460
ItemNoType,
461461
UnderscoreForArrayLengths,
462462
EarlySyntaxWarning,
463+
TypeAscriptionSyntax,
463464
}
464465

465466
fn default_track_diagnostic(_: &Diagnostic) {}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,6 +3262,42 @@ declare_lint! {
32623262
};
32633263
}
32643264

3265+
declare_lint! {
3266+
/// The `type_ascription_syntax` lint detects the use of the old type ascription
3267+
/// syntax which is going to be removed in the future. This syntax was previously
3268+
/// allowed accidentally, despite being unstable, and is now being phased out.
3269+
///
3270+
/// ### Example
3271+
///
3272+
/// ```rust
3273+
/// #[cfg(FALSE)]
3274+
/// let x = 1: u32;
3275+
/// ```
3276+
///
3277+
/// {{produces}}
3278+
///
3279+
/// ### Explanation
3280+
///
3281+
/// The old type ascription syntax was accidentally allowed in the past, but
3282+
/// is now being phased out. The old syntax is too problematic to be stabilized
3283+
/// as-is, and causes an endless tail of diagnostics problems trying to distinguish
3284+
/// it from other syntax.
3285+
///
3286+
///
3287+
///
3288+
/// This is a [future-incompatible] lint to transition this
3289+
/// to a hard error in the future. See [RFC #3307] for more details.
3290+
///
3291+
/// [RFC #3307]: https://github.com/rust-lang/rfcs/pull/3307
3292+
/// [future-incompatible]: ../index.md#future-incompatible-lints
3293+
pub TYPE_ASCRIPTION_SYNTAX,
3294+
Deny,
3295+
"type ascription syntax is going to be removed in the future",
3296+
@future_incompatible = FutureIncompatibleInfo {
3297+
reference: "RFC #3307 <https://github.com/rust-lang/rfcs/pull/3307>",
3298+
};
3299+
}
3300+
32653301
declare_lint_pass! {
32663302
/// Does nothing as a lint pass, but registers some `Lint`s
32673303
/// that are used by other parts of the compiler.
@@ -3331,6 +3367,7 @@ declare_lint_pass! {
33313367
NONTRIVIAL_STRUCTURAL_MATCH,
33323368
SOFT_UNSTABLE,
33333369
UNSTABLE_SYNTAX_PRE_EXPANSION,
3370+
TYPE_ASCRIPTION_SYNTAX,
33343371
INLINE_NO_SANITIZE,
33353372
BAD_ASM_STYLE,
33363373
ASM_SUB_REGISTER,

compiler/rustc_session/src/parse.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ pub fn feature_warn_issue<'a>(
155155
err.stash(span, StashKey::EarlySyntaxWarning);
156156
}
157157

158+
pub fn feature_force_warn<'a>(sess: &'a ParseSess, span: Span, lint: &'static Lint) {
159+
let future_incompatible = lint.future_incompatible.as_ref().unwrap();
160+
let mut err = sess.span_diagnostic.struct_warn(lint.desc);
161+
err.set_span(span);
162+
err.code(DiagnosticId::Lint {
163+
name: lint.name_lower(),
164+
has_future_breakage: true,
165+
is_force_warn: false,
166+
});
167+
err.note(format!("for more information, see {}", future_incompatible.reference));
168+
169+
err.stash(span, StashKey::TypeAscriptionSyntax);
170+
}
171+
158172
/// Adds the diagnostics for a feature to an existing error.
159173
pub fn add_feature_diagnostics<'a>(err: &mut Diagnostic, sess: &'a ParseSess, feature: Symbol) {
160174
add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language);

src/test/ui/consts/auxiliary/external_macro.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
// https://github.com/rust-lang/rust/issues/65300
66

77
#[macro_export]
8-
#[allow_internal_unstable(type_ascription)]
98
macro_rules! static_assert {
109
($test:expr) => {
1110
#[allow(dead_code)]
12-
const _: () = [()][!($test: bool) as usize];
13-
}
11+
const _: () = [()][!($test as bool) as usize];
12+
};
1413
}

src/test/ui/consts/const_in_pattern/accept_structural.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
// See also RFC 1445
1616

17-
#![feature(type_ascription)]
18-
1917
#[derive(Copy, Clone, Debug)]
2018
struct NoPartialEq(u32);
2119

@@ -45,9 +43,6 @@ fn main() {
4543
const TUPLE: (OND, OND) = (None, None);
4644
match (None, None) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
4745

48-
const TYPE_ASCRIPTION: OND = None: OND;
49-
match None { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
50-
5146
const ARRAY: [OND; 2] = [None, None];
5247
match [None; 2] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
5348

src/test/ui/lint/unused/issue-88519-unused-paren.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Make sure unused parens lint doesn't emit a false positive.
33
// See https://github.com/rust-lang/rust/issues/88519
44
#![deny(unused_parens)]
5-
#![feature(type_ascription)]
65

76
// binary ops are tested in issue-71290-unused-paren-binop.rs
87

@@ -49,27 +48,6 @@ mod casts {
4948
}
5049
}
5150

52-
mod typeascription {
53-
fn outside() -> u8 {
54-
({ 0 }): u8
55-
}
56-
fn inside() -> u8 {
57-
({ 0 }: u8)
58-
}
59-
fn outside_match() -> u8 {
60-
(match 0 { x => x }): u8
61-
}
62-
fn inside_match() -> u8 {
63-
(match 0 { x => x }: u8)
64-
}
65-
fn outside_if() -> u8 {
66-
(if false { 0 } else { 0 }): u8
67-
}
68-
fn inside_if() -> u8 {
69-
(if false { 0 } else { 0 }: u8)
70-
}
71-
}
72-
7351
mod index {
7452
fn outside(x: &[u8]) -> u8 {
7553
({ x })[0]

src/test/ui/macros/stringify.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#![feature(raw_ref_op)]
1414
#![feature(trait_alias)]
1515
#![feature(try_blocks)]
16-
#![feature(type_ascription)]
1716
#![deny(unused_macros)]
1817

1918
macro_rules! stringify_block {
@@ -140,7 +139,9 @@ fn test_expr() {
140139

141140
// ExprKind::Type
142141
assert_eq!(stringify_expr!(expr: T), "expr: T");
142+
//~^ WARN: type ascription syntax is going to be removed
143143
assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
144+
//~^ WARN: type ascription syntax is going to be removed
144145

145146
// ExprKind::If
146147
assert_eq!(stringify_expr!(if true {}), "if true {}");

src/test/ui/macros/stringify.stderr

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
warning: type ascription syntax is going to be removed in the future
2+
--> $DIR/stringify.rs:141:32
3+
|
4+
LL | assert_eq!(stringify_expr!(expr: T), "expr: T");
5+
| ^^^^^^^
6+
|
7+
= note: for more information, see RFC #3307 <https://github.com/rust-lang/rfcs/pull/3307>
8+
9+
warning: type ascription syntax is going to be removed in the future
10+
--> $DIR/stringify.rs:143:32
11+
|
12+
LL | assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
13+
| ^^^^^^^^^^^
14+
|
15+
= note: for more information, see RFC #3307 <https://github.com/rust-lang/rfcs/pull/3307>
16+
17+
warning: 2 warnings emitted
18+
19+
Future incompatibility report: Future breakage diagnostic:
20+
warning: type ascription syntax is going to be removed in the future
21+
--> $DIR/stringify.rs:141:32
22+
|
23+
LL | assert_eq!(stringify_expr!(expr: T), "expr: T");
24+
| ^^^^^^^
25+
|
26+
= note: for more information, see RFC #3307 <https://github.com/rust-lang/rfcs/pull/3307>
27+
28+
Future breakage diagnostic:
29+
warning: type ascription syntax is going to be removed in the future
30+
--> $DIR/stringify.rs:143:32
31+
|
32+
LL | assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
33+
| ^^^^^^^^^^^
34+
|
35+
= note: for more information, see RFC #3307 <https://github.com/rust-lang/rfcs/pull/3307>
36+

src/test/ui/mir/mir_ascription_coercion.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/test/ui/raw-ref-op/raw-ref-temp-deref.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,4 @@ fn main() {
1616
let index_deref_ref = &raw const ARRAY_REF[0];
1717
let deref_ref = &raw const *SLICE_REF;
1818
let index_deref_ref = &raw const SLICE_REF[1];
19-
20-
let x = 0;
21-
let ascribe_ref = &raw const (x: i32);
22-
let ascribe_deref = &raw const (*ARRAY_REF: [i32; 2]);
23-
let ascribe_index_deref = &raw const (ARRAY_REF[0]: i32);
2419
}

src/test/ui/reachable/expr_type.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/test/ui/reachable/expr_type.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/test/ui/type/type-ascription.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)