Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3a90bed

Browse files
committedJun 3, 2022
Auto merge of #96296 - cjgillot:remove-label-lt-shadow, r=petrochenkov
Remove label/lifetime shadowing warnings This PR removes some pre-1.0 shadowing warnings for labels and lifetimes. The current behaviour of the compiler is to warn * labels that shadow unrelated labels in the same function --> removed ```rust 'a: loop {} 'a: loop {} // STOP WARNING ``` * labels that shadow enclosing labels --> kept, but only if shadowing is hygienic ```rust 'a: loop { 'a: loop {} // KEEP WARNING } ``` * labels that shadow lifetime --> removed ```rust fn foo<'a>() { 'a: loop {} // STOP WARNING } ``` * lifetimes that shadow labels --> removed ```rust 'a: loop { let b = Box::new(|x: &i8| *x) as Box<dyn for <'a> Fn(&'a i8) -> i8>; // STOP WARNING } ``` * lifetimes that shadow lifetimes --> kept ```rust fn foo<'a>() { let b = Box::new(|x: &i8| *x) as Box<dyn for <'a> Fn(&'a i8) -> i8>; // KEEP WARNING } ``` Closes #31745. ----- From `@petrochenkov` in #95781 (comment) > I think we should remove these silly checks entirely. > They were introduced long time ago in case some new language features appear and require this space. > Now we have another mechanism for such language changes - editions, and if "lifetimes in expressions" or something like that needs to be introduced it could be introduced as an edition change. > However, there was no plans to introduce anything like for years, so it's unlikely that even the edition mechanism will be necessary. r? rust-lang/lang
2 parents f5507aa + 2aa9c70 commit 3a90bed

31 files changed

+223
-1751
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,14 +1848,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18481848
fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> {
18491849
let (name, kind) = match param.kind {
18501850
GenericParamKind::Lifetime => {
1851-
let param_name = if param.ident.name == kw::StaticLifetime
1852-
|| param.ident.name == kw::UnderscoreLifetime
1853-
{
1854-
ParamName::Error
1855-
} else {
1856-
let ident = self.lower_ident(param.ident);
1857-
ParamName::Plain(ident)
1858-
};
1851+
// AST resolution emitted an error on those parameters, so we lower them using
1852+
// `ParamName::Error`.
1853+
let param_name =
1854+
if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
1855+
ParamName::Error
1856+
} else {
1857+
let ident = self.lower_ident(param.ident);
1858+
ParamName::Plain(ident)
1859+
};
18591860
let kind =
18601861
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
18611862

@@ -1880,10 +1881,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18801881
)
18811882
}
18821883
};
1883-
let name = match name {
1884-
hir::ParamName::Plain(ident) => hir::ParamName::Plain(self.lower_ident(ident)),
1885-
name => name,
1886-
};
18871884

18881885
let hir_id = self.lower_node_id(param.id);
18891886
self.lower_attrs(hir_id, &param.attrs);

‎compiler/rustc_error_codes/src/error_codes/E0263.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
A lifetime was declared more than once in the same scope.
24

35
Erroneous code example:
46

5-
```compile_fail,E0263
7+
```compile_fail,E0403
68
fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str, z: &'a str) { // error!
79
}
810
```

‎compiler/rustc_resolve/src/late.rs

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ impl RibKind<'_> {
172172
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
173173
}
174174
}
175+
176+
/// This rib forbids referring to labels defined in upwards ribs.
177+
fn is_label_barrier(self) -> bool {
178+
match self {
179+
NormalRibKind | MacroDefinition(..) => false,
180+
181+
AssocItemRibKind
182+
| ClosureOrAsyncRibKind
183+
| FnItemRibKind
184+
| ItemRibKind(..)
185+
| ConstantItemRibKind(..)
186+
| ModuleRibKind(..)
187+
| ForwardGenericParamBanRibKind
188+
| ConstParamTyRibKind
189+
| InlineAsmSymRibKind => true,
190+
}
191+
}
175192
}
176193

177194
/// A single local scope.
@@ -732,7 +749,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
732749
// Create a value rib for the function.
733750
self.with_rib(ValueNS, rib_kind, |this| {
734751
// Create a label rib for the function.
735-
this.with_label_rib(rib_kind, |this| {
752+
this.with_label_rib(FnItemRibKind, |this| {
736753
let async_node_id = fn_kind.header().and_then(|h| h.asyncness.opt_return_id());
737754

738755
if let FnKind::Fn(_, _, _, _, generics, _) = fn_kind {
@@ -1531,13 +1548,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15311548

15321549
/// Searches the current set of local scopes for labels. Returns the `NodeId` of the resolved
15331550
/// label and reports an error if the label is not found or is unreachable.
1534-
fn resolve_label(&mut self, mut label: Ident) -> Option<NodeId> {
1551+
fn resolve_label(&mut self, mut label: Ident) -> Result<(NodeId, Span), ResolutionError<'a>> {
15351552
let mut suggestion = None;
15361553

1537-
// Preserve the original span so that errors contain "in this macro invocation"
1538-
// information.
1539-
let original_span = label.span;
1540-
15411554
for i in (0..self.label_ribs.len()).rev() {
15421555
let rib = &self.label_ribs[i];
15431556

@@ -1553,18 +1566,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15531566
if let Some((ident, id)) = rib.bindings.get_key_value(&ident) {
15541567
let definition_span = ident.span;
15551568
return if self.is_label_valid_from_rib(i) {
1556-
Some(*id)
1569+
Ok((*id, definition_span))
15571570
} else {
1558-
self.report_error(
1559-
original_span,
1560-
ResolutionError::UnreachableLabel {
1561-
name: label.name,
1562-
definition_span,
1563-
suggestion,
1564-
},
1565-
);
1566-
1567-
None
1571+
Err(ResolutionError::UnreachableLabel {
1572+
name: label.name,
1573+
definition_span,
1574+
suggestion,
1575+
})
15681576
};
15691577
}
15701578

@@ -1573,34 +1581,16 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15731581
suggestion = suggestion.or_else(|| self.suggestion_for_label_in_rib(i, label));
15741582
}
15751583

1576-
self.report_error(
1577-
original_span,
1578-
ResolutionError::UndeclaredLabel { name: label.name, suggestion },
1579-
);
1580-
None
1584+
Err(ResolutionError::UndeclaredLabel { name: label.name, suggestion })
15811585
}
15821586

15831587
/// Determine whether or not a label from the `rib_index`th label rib is reachable.
15841588
fn is_label_valid_from_rib(&self, rib_index: usize) -> bool {
15851589
let ribs = &self.label_ribs[rib_index + 1..];
15861590

15871591
for rib in ribs {
1588-
match rib.kind {
1589-
NormalRibKind | MacroDefinition(..) => {
1590-
// Nothing to do. Continue.
1591-
}
1592-
1593-
AssocItemRibKind
1594-
| ClosureOrAsyncRibKind
1595-
| FnItemRibKind
1596-
| ItemRibKind(..)
1597-
| ConstantItemRibKind(..)
1598-
| ModuleRibKind(..)
1599-
| ForwardGenericParamBanRibKind
1600-
| ConstParamTyRibKind
1601-
| InlineAsmSymRibKind => {
1602-
return false;
1603-
}
1592+
if rib.kind.is_label_barrier() {
1593+
return false;
16041594
}
16051595
}
16061596

@@ -1895,6 +1885,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18951885
let mut function_value_rib = Rib::new(kind);
18961886
let mut function_lifetime_rib = LifetimeRib::new(lifetime_kind);
18971887
let mut seen_bindings = FxHashMap::default();
1888+
// Store all seen lifetimes names from outer scopes.
1889+
let mut seen_lifetimes = FxHashSet::default();
18981890

18991891
// We also can't shadow bindings from the parent item
19001892
if let AssocItemRibKind = kind {
@@ -1910,16 +1902,36 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19101902
add_bindings_for_ns(TypeNS);
19111903
}
19121904

1905+
// Forbid shadowing lifetime bindings
1906+
for rib in self.lifetime_ribs.iter().rev() {
1907+
seen_lifetimes.extend(rib.bindings.iter().map(|(ident, _)| *ident));
1908+
if let LifetimeRibKind::Item = rib.kind {
1909+
break;
1910+
}
1911+
}
1912+
19131913
for param in params {
19141914
let ident = param.ident.normalize_to_macros_2_0();
19151915
debug!("with_generic_param_rib: {}", param.id);
19161916

1917+
if let GenericParamKind::Lifetime = param.kind
1918+
&& let Some(&original) = seen_lifetimes.get(&ident)
1919+
{
1920+
diagnostics::signal_lifetime_shadowing(self.r.session, original, param.ident);
1921+
// Record lifetime res, so lowering knows there is something fishy.
1922+
self.record_lifetime_res(param.id, LifetimeRes::Error);
1923+
continue;
1924+
}
1925+
19171926
match seen_bindings.entry(ident) {
19181927
Entry::Occupied(entry) => {
19191928
let span = *entry.get();
19201929
let err = ResolutionError::NameAlreadyUsedInParameterList(ident.name, span);
1921-
if !matches!(param.kind, GenericParamKind::Lifetime) {
1922-
self.report_error(param.ident.span, err);
1930+
self.report_error(param.ident.span, err);
1931+
if let GenericParamKind::Lifetime = param.kind {
1932+
// Record lifetime res, so lowering knows there is something fishy.
1933+
self.record_lifetime_res(param.id, LifetimeRes::Error);
1934+
continue;
19231935
}
19241936
}
19251937
Entry::Vacant(entry) => {
@@ -1936,6 +1948,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19361948
)
19371949
.span_label(param.ident.span, "`'_` is a reserved lifetime name")
19381950
.emit();
1951+
// Record lifetime res, so lowering knows there is something fishy.
1952+
self.record_lifetime_res(param.id, LifetimeRes::Error);
19391953
continue;
19401954
}
19411955

@@ -1949,6 +1963,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19491963
)
19501964
.span_label(param.ident.span, "'static is a reserved lifetime name")
19511965
.emit();
1966+
// Record lifetime res, so lowering knows there is something fishy.
1967+
self.record_lifetime_res(param.id, LifetimeRes::Error);
19521968
continue;
19531969
}
19541970

@@ -3114,6 +3130,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
31143130
if label.ident.as_str().as_bytes()[1] != b'_' {
31153131
self.diagnostic_metadata.unused_labels.insert(id, label.ident.span);
31163132
}
3133+
3134+
if let Ok((_, orig_span)) = self.resolve_label(label.ident) {
3135+
diagnostics::signal_label_shadowing(self.r.session, orig_span, label.ident)
3136+
}
3137+
31173138
self.with_label_rib(NormalRibKind, |this| {
31183139
let ident = label.ident.normalize_to_macro_rules();
31193140
this.label_ribs.last_mut().unwrap().bindings.insert(ident, id);
@@ -3219,10 +3240,15 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
32193240
}
32203241

32213242
ExprKind::Break(Some(label), _) | ExprKind::Continue(Some(label)) => {
3222-
if let Some(node_id) = self.resolve_label(label.ident) {
3223-
// Since this res is a label, it is never read.
3224-
self.r.label_res_map.insert(expr.id, node_id);
3225-
self.diagnostic_metadata.unused_labels.remove(&node_id);
3243+
match self.resolve_label(label.ident) {
3244+
Ok((node_id, _)) => {
3245+
// Since this res is a label, it is never read.
3246+
self.r.label_res_map.insert(expr.id, node_id);
3247+
self.diagnostic_metadata.unused_labels.remove(&node_id);
3248+
}
3249+
Err(error) => {
3250+
self.report_error(label.ident.span, error);
3251+
}
32263252
}
32273253

32283254
// visit `break` argument if any

‎compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
2525
use rustc_hir::PrimTy;
2626
use rustc_session::lint;
2727
use rustc_session::parse::feature_err;
28+
use rustc_session::Session;
2829
use rustc_span::edition::Edition;
2930
use rustc_span::hygiene::MacroKind;
3031
use rustc_span::lev_distance::find_best_match_for_name;
@@ -2036,6 +2037,34 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
20362037
}
20372038
}
20382039

2040+
/// Report lifetime/lifetime shadowing as an error.
2041+
pub fn signal_lifetime_shadowing(sess: &Session, orig: Ident, shadower: Ident) {
2042+
let mut err = struct_span_err!(
2043+
sess,
2044+
shadower.span,
2045+
E0496,
2046+
"lifetime name `{}` shadows a lifetime name that is already in scope",
2047+
orig.name,
2048+
);
2049+
err.span_label(orig.span, "first declared here");
2050+
err.span_label(shadower.span, format!("lifetime `{}` already in scope", orig.name));
2051+
err.emit();
2052+
}
2053+
2054+
/// Shadowing involving a label is only a warning for historical reasons.
2055+
//FIXME: make this a proper lint.
2056+
pub fn signal_label_shadowing(sess: &Session, orig: Span, shadower: Ident) {
2057+
let name = shadower.name;
2058+
let shadower = shadower.span;
2059+
let mut err = sess.struct_span_warn(
2060+
shadower,
2061+
&format!("label name `{}` shadows a label name that is already in scope", name),
2062+
);
2063+
err.span_label(orig, "first declared here");
2064+
err.span_label(shadower, format!("label `{}` already in scope", name));
2065+
err.emit();
2066+
}
2067+
20392068
impl<'tcx> LifetimeContext<'_, 'tcx> {
20402069
pub(crate) fn report_missing_lifetime_specifiers(
20412070
&self,

‎compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 34 additions & 299 deletions
Large diffs are not rendered by default.

‎src/test/ui/error-codes/E0263.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) {
2-
//~^ ERROR E0263
2+
//~^ ERROR E0403
33
}
44

55
fn main() {}

‎src/test/ui/error-codes/E0263.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0263]: lifetime name `'a` declared twice in the same scope
1+
error[E0403]: the name `'a` is already used for a generic parameter in this item's generic parameters
22
--> $DIR/E0263.rs:1:16
33
|
44
LL | fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) {
5-
| -- ^^ declared twice
5+
| -- ^^ already used
66
| |
7-
| previous declaration here
7+
| first use of `'a`
88

99
error: aborting due to previous error
1010

11-
For more information about this error, try `rustc --explain E0263`.
11+
For more information about this error, try `rustc --explain E0403`.

‎src/test/ui/for-loop-while/label_break_value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn label_break_match(c: u8, xe: u8, ye: i8) {
102102
0 => break 'a 0,
103103
v if { if v % 2 == 0 { break 'a 1; }; v % 3 == 0 } => { x += 1; },
104104
v if { 'b: { break 'b v == 5; } } => { x = 41; },
105-
_ => 'b: { //~ WARNING `'b` shadows a label
105+
_ => 'b: {
106106
break 'b ();
107107
},
108108
}
@@ -128,8 +128,8 @@ fn label_break_macro() {
128128
0
129129
};
130130
assert_eq!(x, 0);
131-
let x: u8 = 'a: { //~ WARNING `'a` shadows a label
132-
'b: { //~ WARNING `'b` shadows a label
131+
let x: u8 = 'a: {
132+
'b: {
133133
if true {
134134
mac1!('a, 1);
135135
}

‎src/test/ui/for-loop-while/label_break_value.stderr

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

‎src/test/ui/for-loop-while/label_break_value_invalid.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ fn lbv_macro_test_hygiene_respected() {
2020
macro_rules! mac3 {
2121
($val:expr) => {
2222
'a: {
23-
//~^ WARNING `'a` shadows a label
24-
//~| WARNING `'a` shadows a label
25-
//~| WARNING `'a` shadows a label
2623
$val
2724
}
2825
};
2926
}
30-
let x: u8 = mac3!('b: { //~ WARNING `'b` shadows a label
27+
let x: u8 = mac3!('b: {
3128
if true {
3229
break 'a 3; //~ ERROR undeclared label `'a` [E0426]
3330
}

‎src/test/ui/for-loop-while/label_break_value_invalid.stderr

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | mac2!(2);
1010
= note: this error originates in the macro `mac2` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

1212
error[E0426]: use of undeclared label `'a`
13-
--> $DIR/label_break_value_invalid.rs:32:19
13+
--> $DIR/label_break_value_invalid.rs:29:19
1414
|
1515
LL | let x: u8 = mac3!('b: {
1616
| -- a label with a similar name is reachable
@@ -22,68 +22,11 @@ LL | break 'a 3;
2222
| help: try using similarly named label: `'b`
2323

2424
error[E0426]: use of undeclared label `'a`
25-
--> $DIR/label_break_value_invalid.rs:37:29
25+
--> $DIR/label_break_value_invalid.rs:34:29
2626
|
2727
LL | let x: u8 = mac3!(break 'a 4);
2828
| ^^ undeclared label `'a`
2929

30-
warning: label name `'a` shadows a label name that is already in scope
31-
--> $DIR/label_break_value_invalid.rs:22:13
32-
|
33-
LL | let x: u8 = 'a: {
34-
| -- first declared here
35-
...
36-
LL | 'a: {
37-
| ^^ label `'a` already in scope
38-
...
39-
LL | let x: u8 = mac3!('b: {
40-
| _________________-
41-
LL | | if true {
42-
LL | | break 'a 3;
43-
LL | | }
44-
LL | | 0
45-
LL | | });
46-
| |______- in this macro invocation
47-
|
48-
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
49-
50-
warning: label name `'b` shadows a label name that is already in scope
51-
--> $DIR/label_break_value_invalid.rs:30:23
52-
|
53-
LL | 'b: {
54-
| -- first declared here
55-
...
56-
LL | let x: u8 = mac3!('b: {
57-
| ^^ label `'b` already in scope
58-
59-
warning: label name `'a` shadows a label name that is already in scope
60-
--> $DIR/label_break_value_invalid.rs:22:13
61-
|
62-
LL | let x: u8 = 'a: {
63-
| -- first declared here
64-
...
65-
LL | 'a: {
66-
| ^^ label `'a` already in scope
67-
...
68-
LL | let x: u8 = mac3!(break 'a 4);
69-
| ----------------- in this macro invocation
70-
|
71-
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
72-
73-
warning: label name `'a` shadows a label name that is already in scope
74-
--> $DIR/label_break_value_invalid.rs:22:13
75-
|
76-
LL | 'a: {
77-
| ^^
78-
| |
79-
| first declared here
80-
| label `'a` already in scope
81-
...
82-
LL | let x: u8 = mac3!(break 'a 4);
83-
| ----------------- in this macro invocation
84-
|
85-
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
86-
87-
error: aborting due to 3 previous errors; 4 warnings emitted
30+
error: aborting due to 3 previous errors
8831

8932
For more information about this error, try `rustc --explain E0426`.

‎src/test/ui/generic-associated-types/shadowing.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
2+
--> $DIR/shadowing.rs:4:14
3+
|
4+
LL | trait Shadow<'a> {
5+
| -- first declared here
6+
LL | type Bar<'a>;
7+
| ^^ lifetime `'a` already in scope
8+
9+
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
10+
--> $DIR/shadowing.rs:13:14
11+
|
12+
LL | impl<'a> NoShadow<'a> for &'a u32 {
13+
| -- first declared here
14+
LL | type Bar<'a> = i32;
15+
| ^^ lifetime `'a` already in scope
16+
117
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
218
--> $DIR/shadowing.rs:18:14
319
|
@@ -14,22 +30,6 @@ LL | impl<T> NoShadowT<T> for Option<T> {
1430
LL | type Bar<T> = i32;
1531
| ^ already used
1632

17-
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
18-
--> $DIR/shadowing.rs:13:14
19-
|
20-
LL | impl<'a> NoShadow<'a> for &'a u32 {
21-
| -- first declared here
22-
LL | type Bar<'a> = i32;
23-
| ^^ lifetime `'a` already in scope
24-
25-
error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
26-
--> $DIR/shadowing.rs:4:14
27-
|
28-
LL | trait Shadow<'a> {
29-
| -- first declared here
30-
LL | type Bar<'a>;
31-
| ^^ lifetime `'a` already in scope
32-
3333
error: aborting due to 4 previous errors
3434

3535
Some errors have detailed explanations: E0403, E0496.

‎src/test/ui/hygiene/duplicate_lifetimes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
#[rustc_macro_transparency = "semitransparent"]
77
macro m($a:lifetime) {
8-
fn g<$a, 'a>() {} //~ ERROR lifetime name `'a` declared twice
8+
fn g<$a, 'a>() {} //~ ERROR the name `'a` is already used for a generic parameter
99
}
1010

1111
#[rustc_macro_transparency = "transparent"]
1212
macro n($a:lifetime) {
13-
fn h<$a, 'a>() {} //~ ERROR lifetime name `'a` declared twice
13+
fn h<$a, 'a>() {} //~ ERROR the name `'a` is already used for a generic parameter
1414
}
1515

1616
m!('a);
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
error[E0263]: lifetime name `'a` declared twice in the same scope
1+
error[E0403]: the name `'a` is already used for a generic parameter in this item's generic parameters
22
--> $DIR/duplicate_lifetimes.rs:8:14
33
|
44
LL | fn g<$a, 'a>() {}
5-
| ^^ declared twice
5+
| ^^ already used
66
...
77
LL | m!('a);
88
| ------
99
| | |
10-
| | previous declaration here
10+
| | first use of `'a`
1111
| in this macro invocation
1212
|
1313
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

15-
error[E0263]: lifetime name `'a` declared twice in the same scope
15+
error[E0403]: the name `'a` is already used for a generic parameter in this item's generic parameters
1616
--> $DIR/duplicate_lifetimes.rs:13:14
1717
|
1818
LL | fn h<$a, 'a>() {}
19-
| ^^ declared twice
19+
| ^^ already used
2020
...
2121
LL | n!('a);
2222
| ------
2323
| | |
24-
| | previous declaration here
24+
| | first use of `'a`
2525
| in this macro invocation
2626
|
2727
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
2828

2929
error: aborting due to 2 previous errors
3030

31-
For more information about this error, try `rustc --explain E0263`.
31+
For more information about this error, try `rustc --explain E0403`.

‎src/test/ui/hygiene/hygienic-labels-in-let.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,28 @@
1313
macro_rules! loop_x {
1414
($e: expr) => {
1515
// $e shouldn't be able to interact with this 'x
16-
'x: loop { $e }
17-
//~^ WARNING shadows a label name that is already in scope
18-
//~| WARNING shadows a label name that is already in scope
19-
//~| WARNING shadows a label name that is already in scope
20-
//~| WARNING shadows a label name that is already in scope
21-
}
16+
'x: loop {
17+
$e
18+
}
19+
};
2220
}
2321

2422
macro_rules! while_true {
2523
($e: expr) => {
2624
// $e shouldn't be able to interact with this 'x
27-
'x: while 1 + 1 == 2 { $e }
28-
//~^ WARNING shadows a label name that is already in scope
29-
//~| WARNING shadows a label name that is already in scope
30-
//~| WARNING shadows a label name that is already in scope
31-
//~| WARNING shadows a label name that is already in scope
32-
//~| WARNING shadows a label name that is already in scope
33-
}
25+
'x: while 1 + 1 == 2 {
26+
$e
27+
}
28+
};
3429
}
3530

3631
macro_rules! run_once {
3732
($e: expr) => {
3833
// ditto
39-
'x: for _ in 0..1 { $e }
40-
//~^ WARNING shadows a label name that is already in scope
41-
//~| WARNING shadows a label name that is already in scope
42-
//~| WARNING shadows a label name that is already in scope
43-
//~| WARNING shadows a label name that is already in scope
44-
//~| WARNING shadows a label name that is already in scope
45-
//~| WARNING shadows a label name that is already in scope
46-
//~| WARNING shadows a label name that is already in scope
47-
}
34+
'x: for _ in 0..1 {
35+
$e
36+
}
37+
};
4838
}
4939

5040
pub fn main() {
@@ -62,8 +52,6 @@ pub fn main() {
6252

6353
let k: isize = {
6454
'x: for _ in 0..1 {
65-
//~^ WARNING shadows a label name that is already in scope
66-
//~| WARNING shadows a label name that is already in scope
6755
// ditto
6856
loop_x!(break 'x);
6957
i += 1;
@@ -74,10 +62,6 @@ pub fn main() {
7462

7563
let l: isize = {
7664
'x: for _ in 0..1 {
77-
//~^ WARNING shadows a label name that is already in scope
78-
//~| WARNING shadows a label name that is already in scope
79-
//~| WARNING shadows a label name that is already in scope
80-
//~| WARNING shadows a label name that is already in scope
8165
// ditto
8266
while_true!(break 'x);
8367
i += 1;
@@ -88,12 +72,6 @@ pub fn main() {
8872

8973
let n: isize = {
9074
'x: for _ in 0..1 {
91-
//~^ WARNING shadows a label name that is already in scope
92-
//~| WARNING shadows a label name that is already in scope
93-
//~| WARNING shadows a label name that is already in scope
94-
//~| WARNING shadows a label name that is already in scope
95-
//~| WARNING shadows a label name that is already in scope
96-
//~| WARNING shadows a label name that is already in scope
9775
// ditto
9876
run_once!(continue 'x);
9977
i += 1;

‎src/test/ui/hygiene/hygienic-labels-in-let.stderr

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

‎src/test/ui/hygiene/hygienic-labels.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,28 @@
1010
macro_rules! loop_x {
1111
($e: expr) => {
1212
// $e shouldn't be able to interact with this 'x
13-
'x: loop { $e }
14-
//~^ WARNING shadows a label name that is already in scope
15-
//~| WARNING shadows a label name that is already in scope
16-
//~| WARNING shadows a label name that is already in scope
17-
//~| WARNING shadows a label name that is already in scope
18-
}
13+
'x: loop {
14+
$e
15+
}
16+
};
1917
}
2018

2119
macro_rules! run_once {
2220
($e: expr) => {
2321
// ditto
24-
'x: for _ in 0..1 { $e }
25-
//~^ WARNING shadows a label name that is already in scope
26-
//~| WARNING shadows a label name that is already in scope
27-
//~| WARNING shadows a label name that is already in scope
28-
//~| WARNING shadows a label name that is already in scope
29-
//~| WARNING shadows a label name that is already in scope
30-
//~| WARNING shadows a label name that is already in scope
31-
//~| WARNING shadows a label name that is already in scope
32-
}
22+
'x: for _ in 0..1 {
23+
$e
24+
}
25+
};
3326
}
3427

3528
macro_rules! while_x {
3629
($e: expr) => {
3730
// ditto
38-
'x: while 1 + 1 == 2 { $e }
39-
//~^ WARNING shadows a label name that is already in scope
40-
//~| WARNING shadows a label name that is already in scope
41-
//~| WARNING shadows a label name that is already in scope
42-
//~| WARNING shadows a label name that is already in scope
43-
//~| WARNING shadows a label name that is already in scope
44-
}
31+
'x: while 1 + 1 == 2 {
32+
$e
33+
}
34+
};
4535
}
4636

4737
pub fn main() {
@@ -52,32 +42,17 @@ pub fn main() {
5242
}
5343

5444
'x: loop {
55-
//~^ WARNING shadows a label name that is already in scope
56-
//~| WARNING shadows a label name that is already in scope
57-
5845
// ditto
5946
loop_x!(break 'x);
6047
panic!("break doesn't act hygienically inside infinite loop");
6148
}
6249

6350
'x: while 1 + 1 == 2 {
64-
//~^ WARNING shadows a label name that is already in scope
65-
//~| WARNING shadows a label name that is already in scope
66-
//~| WARNING shadows a label name that is already in scope
67-
//~| WARNING shadows a label name that is already in scope
68-
6951
while_x!(break 'x);
7052
panic!("break doesn't act hygienically inside infinite while loop");
7153
}
7254

7355
'x: for _ in 0..1 {
74-
//~^ WARNING shadows a label name that is already in scope
75-
//~| WARNING shadows a label name that is already in scope
76-
//~| WARNING shadows a label name that is already in scope
77-
//~| WARNING shadows a label name that is already in scope
78-
//~| WARNING shadows a label name that is already in scope
79-
//~| WARNING shadows a label name that is already in scope
80-
8156
// ditto
8257
run_once!(continue 'x);
8358
panic!("continue doesn't act hygienically inside for loop");

‎src/test/ui/hygiene/hygienic-labels.stderr

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

‎src/test/ui/lint/unused_labels.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
warning: label name `'many_used_shadowed` shadows a label name that is already in scope
2+
--> $DIR/unused_labels.rs:62:9
3+
|
4+
LL | 'many_used_shadowed: for _ in 0..10 {
5+
| ------------------- first declared here
6+
LL |
7+
LL | 'many_used_shadowed: for _ in 0..10 {
8+
| ^^^^^^^^^^^^^^^^^^^ label `'many_used_shadowed` already in scope
9+
110
warning: unused label
211
--> $DIR/unused_labels.rs:11:5
312
|
@@ -52,14 +61,5 @@ warning: unused label
5261
LL | 'unused_block_label: {
5362
| ^^^^^^^^^^^^^^^^^^^
5463

55-
warning: label name `'many_used_shadowed` shadows a label name that is already in scope
56-
--> $DIR/unused_labels.rs:62:9
57-
|
58-
LL | 'many_used_shadowed: for _ in 0..10 {
59-
| ------------------- first declared here
60-
LL |
61-
LL | 'many_used_shadowed: for _ in 0..10 {
62-
| ^^^^^^^^^^^^^^^^^^^ label `'many_used_shadowed` already in scope
63-
6464
warning: 9 warnings emitted
6565

‎src/test/ui/loops/loops-reject-duplicate-labels-2.rs

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

‎src/test/ui/loops/loops-reject-duplicate-labels-2.stderr

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

‎src/test/ui/loops/loops-reject-duplicate-labels.rs

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

‎src/test/ui/loops/loops-reject-duplicate-labels.stderr

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

‎src/test/ui/loops/loops-reject-labels-shadowing-lifetimes.rs

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

‎src/test/ui/loops/loops-reject-labels-shadowing-lifetimes.stderr

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

‎src/test/ui/loops/loops-reject-lifetime-shadowing-label.rs

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

‎src/test/ui/loops/loops-reject-lifetime-shadowing-label.stderr

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

‎src/test/ui/macros/macro-lifetime-used-with-labels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! br {
1818
}
1919
macro_rules! br2 {
2020
($b:lifetime) => {
21-
'b: loop { //~ WARNING `'b` shadows a label name that is already in scope
21+
'b: loop {
2222
break $b; // this $b should refer to the outer loop.
2323
}
2424
}

‎src/test/ui/macros/macro-lifetime-used-with-labels.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
struct Foo<'a, 'a> { //~ ERROR lifetime name `'a` declared twice
2-
x: &'a isize
1+
struct Foo<'a, 'a> {
2+
//~^ ERROR the name `'a` is already used for a generic parameter
3+
x: &'a isize,
34
}
45

56
fn main() {}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0263]: lifetime name `'a` declared twice in the same scope
1+
error[E0403]: the name `'a` is already used for a generic parameter in this item's generic parameters
22
--> $DIR/regions-name-duplicated.rs:1:16
33
|
44
LL | struct Foo<'a, 'a> {
5-
| -- ^^ declared twice
5+
| -- ^^ already used
66
| |
7-
| previous declaration here
7+
| first use of `'a`
88

99
error: aborting due to previous error
1010

11-
For more information about this error, try `rustc --explain E0263`.
11+
For more information about this error, try `rustc --explain E0403`.

0 commit comments

Comments
 (0)
Please sign in to comment.