Skip to content

Commit 1bd30ce

Browse files
committed
Auto merge of #66449 - tmandry:rollup-3p1t0sb, r=tmandry
Rollup of 4 pull requests Successful merges: - #66197 (Push `ast::{ItemKind, ImplItemKind}::OpaqueTy` hack down into lowering) - #66429 (Add a regression test for #62524) - #66435 (Correct `const_in_array_repeat_expressions` feature name) - #66443 (Port erased cleanup) Failed merges: r? @ghost
2 parents ce36ab2 + 0f2f03f commit 1bd30ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+494
-346
lines changed

src/librustc/hir/lowering.rs

-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ impl<'a> LoweringContext<'a> {
454454
| ItemKind::Union(_, ref generics)
455455
| ItemKind::Enum(_, ref generics)
456456
| ItemKind::TyAlias(_, ref generics)
457-
| ItemKind::OpaqueTy(_, ref generics)
458457
| ItemKind::Trait(_, _, ref generics, ..) => {
459458
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
460459
let count = generics

src/librustc/hir/lowering/item.rs

+35-27
Original file line numberDiff line numberDiff line change
@@ -337,20 +337,22 @@ impl LoweringContext<'_> {
337337
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
338338
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
339339
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
340-
ItemKind::TyAlias(ref t, ref generics) => hir::ItemKind::TyAlias(
341-
self.lower_ty(t, ImplTraitContext::disallowed()),
342-
self.lower_generics(generics, ImplTraitContext::disallowed()),
343-
),
344-
ItemKind::OpaqueTy(ref b, ref generics) => hir::ItemKind::OpaqueTy(
345-
hir::OpaqueTy {
346-
generics: self.lower_generics(generics,
347-
ImplTraitContext::OpaqueTy(None)),
348-
bounds: self.lower_param_bounds(b,
349-
ImplTraitContext::OpaqueTy(None)),
350-
impl_trait_fn: None,
351-
origin: hir::OpaqueTyOrigin::TypeAlias,
340+
ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() {
341+
None => {
342+
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
343+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
344+
hir::ItemKind::TyAlias(ty, generics)
352345
},
353-
),
346+
Some(bounds) => {
347+
let ty = hir::OpaqueTy {
348+
generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)),
349+
bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)),
350+
impl_trait_fn: None,
351+
origin: hir::OpaqueTyOrigin::TypeAlias,
352+
};
353+
hir::ItemKind::OpaqueTy(ty)
354+
}
355+
}
354356
ItemKind::Enum(ref enum_definition, ref generics) => {
355357
hir::ItemKind::Enum(
356358
hir::EnumDef {
@@ -916,16 +918,20 @@ impl LoweringContext<'_> {
916918

917919
(generics, hir::ImplItemKind::Method(sig, body_id))
918920
}
919-
ImplItemKind::TyAlias(ref ty) => (
920-
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
921-
hir::ImplItemKind::TyAlias(self.lower_ty(ty, ImplTraitContext::disallowed())),
922-
),
923-
ImplItemKind::OpaqueTy(ref bounds) => (
924-
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
925-
hir::ImplItemKind::OpaqueTy(
926-
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
927-
),
928-
),
921+
ImplItemKind::TyAlias(ref ty) => {
922+
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
923+
let kind = match ty.kind.opaque_top_hack() {
924+
None => {
925+
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
926+
hir::ImplItemKind::TyAlias(ty)
927+
}
928+
Some(bs) => {
929+
let bounds = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
930+
hir::ImplItemKind::OpaqueTy(bounds)
931+
}
932+
};
933+
(generics, kind)
934+
},
929935
ImplItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"),
930936
};
931937

@@ -950,11 +956,13 @@ impl LoweringContext<'_> {
950956
span: i.span,
951957
vis: self.lower_visibility(&i.vis, Some(i.id)),
952958
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
953-
kind: match i.kind {
959+
kind: match &i.kind {
954960
ImplItemKind::Const(..) => hir::AssocItemKind::Const,
955-
ImplItemKind::TyAlias(..) => hir::AssocItemKind::Type,
956-
ImplItemKind::OpaqueTy(..) => hir::AssocItemKind::OpaqueTy,
957-
ImplItemKind::Method(ref sig, _) => hir::AssocItemKind::Method {
961+
ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() {
962+
None => hir::AssocItemKind::Type,
963+
Some(_) => hir::AssocItemKind::OpaqueTy,
964+
},
965+
ImplItemKind::Method(sig, _) => hir::AssocItemKind::Method {
958966
has_self: sig.decl.has_self(),
959967
},
960968
ImplItemKind::Macro(..) => unimplemented!(),

src/librustc/hir/map/def_collector.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
107107
}
108108
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
109109
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
110-
ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
110+
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
111111
ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
112112
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => {
113113
return self.visit_async_fn(
@@ -239,8 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
239239
}
240240
ImplItemKind::Method(..) |
241241
ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name),
242-
ImplItemKind::TyAlias(..) |
243-
ImplItemKind::OpaqueTy(..) => DefPathData::TypeNs(ii.ident.name),
242+
ImplItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name),
244243
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
245244
};
246245

src/librustc/traits/error_reporting.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2176,15 +2176,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21762176
err.note(&format!("required by cast to type `{}`",
21772177
self.ty_to_string(target)));
21782178
}
2179-
ObligationCauseCode::RepeatVec(suggest_const_in_array_repeat_expression) => {
2179+
ObligationCauseCode::RepeatVec(suggest_const_in_array_repeat_expressions) => {
21802180
err.note("the `Copy` trait is required because the \
21812181
repeated element will be copied");
2182-
if suggest_const_in_array_repeat_expression {
2182+
if suggest_const_in_array_repeat_expressions {
21832183
err.note("this array initializer can be evaluated at compile-time, for more \
21842184
information, see issue \
21852185
https://github.com/rust-lang/rust/issues/49147");
21862186
if tcx.sess.opts.unstable_features.is_nightly_build() {
2187-
err.help("add `#![feature(const_in_array_repeat_expression)]` to the \
2187+
err.help("add `#![feature(const_in_array_repeat_expressions)]` to the \
21882188
crate attributes to enable");
21892189
}
21902190
}

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub enum ObligationCauseCode<'tcx> {
206206
SizedReturnType,
207207
/// Yield type must be Sized
208208
SizedYieldType,
209-
/// [T,..n] --> T must be Copy. If `true`, suggest `const_in_array_repeat_expression` feature
209+
/// [T,..n] --> T must be Copy. If `true`, suggest `const_in_array_repeat_expressions` feature
210210
/// flag.
211211
RepeatVec(bool),
212212

src/librustc_error_codes/error_codes.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Error messages for EXXXX errors. Each message should start and end with a
22
// new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
33
// use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
4+
//
5+
// /!\ IMPORTANT /!\
6+
//
7+
// Error messages' format must follow the RFC 1567 available here:
8+
// https://github.com/rust-lang/rfcs/pull/1567
49

510
crate::register_diagnostics! {
611

src/librustc_error_codes/error_codes/E0023.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
A pattern used to match against an enum variant must provide a sub-pattern for
2-
each field of the enum variant. This error indicates that a pattern attempted to
3-
extract an incorrect number of fields from a variant.
1+
A pattern attempted to extract an incorrect number of fields from a variant.
2+
3+
Erroneous code example:
44

55
```
66
enum Fruit {
@@ -9,6 +9,9 @@ enum Fruit {
99
}
1010
```
1111

12+
A pattern used to match against an enum variant must provide a sub-pattern for
13+
each field of the enum variant.
14+
1215
Here the `Apple` variant has two fields, and should be matched against like so:
1316

1417
```

src/librustc_error_codes/error_codes/E0025.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Each field of a struct can only be bound once in a pattern. Erroneous code
2-
example:
1+
Each field of a struct can only be bound once in a pattern.
2+
3+
Erroneous code example:
34

45
```compile_fail,E0025
56
struct Foo {
+10-27
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
1-
This error indicates that a struct pattern attempted to extract a non-existent
2-
field from a struct. Struct fields are identified by the name used before the
3-
colon `:` so struct patterns should resemble the declaration of the struct type
4-
being matched.
1+
A struct pattern attempted to extract a non-existent field from a struct.
52

6-
```
7-
// Correct matching.
8-
struct Thing {
9-
x: u32,
10-
y: u32
11-
}
12-
13-
let thing = Thing { x: 1, y: 2 };
14-
15-
match thing {
16-
Thing { x: xfield, y: yfield } => {}
17-
}
18-
```
19-
20-
If you are using shorthand field patterns but want to refer to the struct field
21-
by a different name, you should rename it explicitly.
22-
23-
Change this:
3+
Erroneous code example:
244

255
```compile_fail,E0026
266
struct Thing {
277
x: u32,
28-
y: u32
8+
y: u32,
299
}
3010
3111
let thing = Thing { x: 0, y: 0 };
3212
3313
match thing {
34-
Thing { x, z } => {}
14+
Thing { x, z } => {} // error: `Thing::z` field doesn't exist
3515
}
3616
```
3717

38-
To this:
18+
If you are using shorthand field patterns but want to refer to the struct field
19+
by a different name, you should rename it explicitly. Struct fields are
20+
identified by the name used before the colon `:` so struct patterns should
21+
resemble the declaration of the struct type being matched.
3922

4023
```
4124
struct Thing {
4225
x: u32,
43-
y: u32
26+
y: u32,
4427
}
4528
4629
let thing = Thing { x: 0, y: 0 };
4730
4831
match thing {
49-
Thing { x, y: z } => {}
32+
Thing { x, y: z } => {} // we renamed `y` to `z`
5033
}
5134
```

src/librustc_error_codes/error_codes/E0027.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
This error indicates that a pattern for a struct fails to specify a sub-pattern
2-
for every one of the struct's fields. Ensure that each field from the struct's
3-
definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
1+
A pattern for a struct fails to specify a sub-pattern for every one of the
2+
struct's fields.
43

5-
For example:
4+
Erroneous code example:
65

76
```compile_fail,E0027
87
struct Dog {
@@ -18,7 +17,8 @@ match d {
1817
}
1918
```
2019

21-
This is correct (explicit):
20+
To fix this error, ensure that each field from the struct's definition is
21+
mentioned in the pattern, or use `..` to ignore unwanted fields. Example:
2222

2323
```
2424
struct Dog {

src/librustc_error_codes/error_codes/E0029.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
In a match expression, only numbers and characters can be matched against a
2-
range. This is because the compiler checks that the range is non-empty at
3-
compile-time, and is unable to evaluate arbitrary comparison functions. If you
4-
want to capture values of an orderable type between two end-points, you can use
5-
a guard.
1+
Something other than numbers and characters has been used for a range.
2+
3+
Erroneous code example:
64

75
```compile_fail,E0029
86
let string = "salutations !";
@@ -20,3 +18,9 @@ match string {
2018
_ => {}
2119
}
2220
```
21+
22+
In a match expression, only numbers and characters can be matched against a
23+
range. This is because the compiler checks that the range is non-empty at
24+
compile-time, and is unable to evaluate arbitrary comparison functions. If you
25+
want to capture values of an orderable type between two end-points, you can use
26+
a guard.

src/librustc_error_codes/error_codes/E0033.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
This error indicates that a pointer to a trait type cannot be implicitly
2-
dereferenced by a pattern. Every trait defines a type, but because the
3-
size of trait implementers isn't fixed, this type has no compile-time size.
4-
Therefore, all accesses to trait types must be through pointers. If you
5-
encounter this error you should try to avoid dereferencing the pointer.
1+
A trait type has been dereferenced.
2+
3+
Erroneous code example:
64

75
```compile_fail,E0033
86
# trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
@@ -17,7 +15,13 @@ trait_obj.method_one();
1715
trait_obj.method_two();
1816
```
1917

18+
A pointer to a trait type cannot be implicitly dereferenced by a pattern. Every
19+
trait defines a type, but because the size of trait implementers isn't fixed,
20+
this type has no compile-time size. Therefore, all accesses to trait types must
21+
be through pointers. If you encounter this error you should try to avoid
22+
dereferencing the pointer.
23+
2024
You can read more about trait objects in the [Trait Objects] section of the
2125
Reference.
2226

23-
[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects
27+
[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects

src/librustc_error_codes/error_codes/E0034.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
The compiler doesn't know what method to call because more than one method
2-
has the same prototype. Erroneous code example:
2+
has the same prototype.
3+
4+
Erroneous code example:
35

46
```compile_fail,E0034
57
struct Test;

src/librustc_error_codes/error_codes/E0040.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
It is not allowed to manually call destructors in Rust. It is also not
2-
necessary to do this since `drop` is called automatically whenever a value goes
3-
out of scope.
1+
It is not allowed to manually call destructors in Rust.
42

5-
Here's an example of this error:
3+
Erroneous code example:
64

75
```compile_fail,E0040
86
struct Foo {
@@ -20,3 +18,22 @@ fn main() {
2018
x.drop(); // error: explicit use of destructor method
2119
}
2220
```
21+
22+
It is unnecessary to do this since `drop` is called automatically whenever a
23+
value goes out of scope. However, if you really need to drop a value by hand,
24+
you can use the `std::mem::drop` function:
25+
26+
```
27+
struct Foo {
28+
x: i32,
29+
}
30+
impl Drop for Foo {
31+
fn drop(&mut self) {
32+
println!("kaboom");
33+
}
34+
}
35+
fn main() {
36+
let mut x = Foo { x: -7 };
37+
drop(x); // ok!
38+
}
39+
```

src/librustc_error_codes/error_codes/E0044.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
You cannot use type or const parameters on foreign items.
2+
23
Example of erroneous code:
34

45
```compile_fail,E0044

src/librustc_error_codes/error_codes/E0045.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
Rust only supports variadic parameters for interoperability with C code in its
2-
FFI. As such, variadic parameters can only be used with functions which are
3-
using the C ABI. Examples of erroneous code:
4-
5-
```compile_fail
6-
#![feature(unboxed_closures)]
1+
Variadic parameters have been used on a non-C ABI function.
72

8-
extern "rust-call" { fn foo(x: u8, ...); }
3+
Erroneous code example:
94

10-
// or
5+
```compile_fail,E0045
6+
#![feature(unboxed_closures)]
117
12-
fn foo(x: u8, ...) {}
8+
extern "rust-call" {
9+
fn foo(x: u8, ...); // error!
10+
}
1311
```
1412

15-
To fix such code, put them in an extern "C" block:
13+
Rust only supports variadic parameters for interoperability with C code in its
14+
FFI. As such, variadic parameters can only be used with functions which are
15+
using the C ABI. To fix such code, put them in an extern "C" block:
1616

1717
```
1818
extern "C" {

0 commit comments

Comments
 (0)