2024-02-24 02:52:59 -05:00
|
|
|
use core::ops::ControlFlow;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2022-06-28 13:10:36 +10:00
|
|
|
use rustc_ast as ast;
|
2024-05-15 10:01:06 -05:00
|
|
|
use rustc_ast::visit::visit_opt;
|
2024-02-24 15:22:42 -05:00
|
|
|
use rustc_ast::{EnumDef, VariantData, attr};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{ErrorGuaranteed, Ident, Span, kw, sym};
|
2021-06-29 20:22:52 -04:00
|
|
|
use smallvec::SmallVec;
|
2022-11-23 11:55:16 +11:00
|
|
|
use thin_vec::{ThinVec, thin_vec};
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
use crate::deriving::generic::ty::*;
|
|
|
|
|
use crate::deriving::generic::*;
|
2023-04-08 20:37:41 +01:00
|
|
|
use crate::errors;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_deriving_default(
|
2024-03-28 12:49:18 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2015-03-26 18:07:49 -07:00
|
|
|
span: Span,
|
2022-06-28 13:10:36 +10:00
|
|
|
mitem: &ast::MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2018-07-12 11:58:16 +02:00
|
|
|
push: &mut dyn FnMut(Annotatable),
|
2022-09-20 11:55:07 +00:00
|
|
|
is_const: bool,
|
2018-07-12 11:58:16 +02:00
|
|
|
) {
|
2021-07-17 01:05:57 -04:00
|
|
|
item.visit_with(&mut DetectNonVariantDefaultAttr { cx });
|
|
|
|
|
|
2013-09-11 21:51:13 -07:00
|
|
|
let trait_def = TraitDef {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2020-07-14 15:05:26 +10:00
|
|
|
path: Path::new(vec![kw::Default, sym::Default]),
|
2022-08-26 14:59:12 +02:00
|
|
|
skip_path_as_bound: has_a_default_variant(item),
|
Allow more deriving on packed structs.
Currently, deriving on packed structs has some non-trivial limitations,
related to the fact that taking references on unaligned fields is UB.
The current approach to field accesses in derived code:
- Normal case: `&self.0`
- In a packed struct that derives `Copy`: `&{self.0}`
- In a packed struct that doesn't derive `Copy`: `&self.0`
Plus, we disallow deriving any builtin traits other than `Default` for any
packed generic type, because it's possible that there might be
misaligned fields. This is a fairly broad restriction.
Plus, we disallow deriving any builtin traits other than `Default` for most
packed types that don't derive `Copy`. (The exceptions are those where the
alignments inherently satisfy the packing, e.g. in a type with
`repr(packed(N))` where all the fields have alignments of `N` or less
anyway. Such types are pretty strange, because the `packed` attribute is
not having any effect.)
This commit introduces a new, simpler approach to field accesses:
- Normal case: `&self.0`
- In a packed struct: `&{self.0}`
In the latter case, this requires that all fields impl `Copy`, which is
a new restriction. This means that the following example compiles under
the old approach and doesn't compile under the new approach.
```
#[derive(Debug)]
struct NonCopy(u8);
#[derive(Debug)
#[repr(packed)]
struct MyType(NonCopy);
```
(Note that the old approach's support for cases like this was brittle.
Changing the `u8` to a `u16` would be enough to stop it working. So not
much capability is lost here.)
However, the other constraints from the old rules are removed. We can now
derive builtin traits for packed generic structs like this:
```
trait Trait { type A; }
#[derive(Hash)]
#[repr(packed)]
pub struct Foo<T: Trait>(T, T::A);
```
To allow this, we add a `T: Copy` bound in the derived impl and a `T::A:
Copy` bound in where clauses. So `T` and `T::A` must impl `Copy`.
We can now also derive builtin traits for packed structs that don't derive
`Copy`, so long as the fields impl `Copy`:
```
#[derive(Hash)]
#[repr(packed)]
pub struct Foo(u32);
```
This includes types that hand-impl `Copy` rather than deriving it, such as the
following, that show up in winapi-0.2:
```
#[derive(Clone)]
#[repr(packed)]
struct MyType(i32);
impl Copy for MyType {}
```
The new approach is simpler to understand and implement, and it avoids
the need for the `unsafe_derive_on_repr_packed` check.
One exception is required for backwards-compatibility: we allow `[u8]`
fields for now. There is a new lint for this,
`byte_slice_in_packed_struct_with_derive`.
2022-11-21 14:40:32 +11:00
|
|
|
needs_copy_as_bound_if_packed: false,
|
2014-02-28 13:09:09 -08:00
|
|
|
additional_bounds: Vec::new(),
|
2016-08-29 11:14:25 +00:00
|
|
|
supports_unions: false,
|
2016-07-19 23:02:06 +05:30
|
|
|
methods: vec![MethodDef {
|
2020-07-08 11:04:10 +10:00
|
|
|
name: kw::Default,
|
2020-07-14 16:19:44 +10:00
|
|
|
generics: Bounds::empty(),
|
2022-06-30 09:15:07 +10:00
|
|
|
explicit_self: false,
|
2022-07-05 08:25:47 +10:00
|
|
|
nonself_args: Vec::new(),
|
2016-07-19 23:02:06 +05:30
|
|
|
ret_ty: Self_,
|
2023-05-26 06:59:17 +10:00
|
|
|
attributes: thin_vec![cx.attr_word(sym::inline, span)],
|
2023-01-14 21:16:25 +00:00
|
|
|
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
|
2021-06-29 20:22:52 -04:00
|
|
|
combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
|
|
|
|
|
match substr.fields {
|
|
|
|
|
StaticStruct(_, fields) => {
|
|
|
|
|
default_struct_substructure(cx, trait_span, substr, fields)
|
|
|
|
|
}
|
2025-01-07 18:28:42 +01:00
|
|
|
StaticEnum(enum_def) => {
|
2024-12-21 02:43:09 +00:00
|
|
|
default_enum_substructure(cx, trait_span, enum_def, item.span())
|
|
|
|
|
}
|
2023-12-18 20:54:03 +11:00
|
|
|
_ => cx.dcx().span_bug(trait_span, "method in `derive(Default)`"),
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
2016-07-19 23:02:06 +05:30
|
|
|
})),
|
|
|
|
|
}],
|
2015-01-25 00:29:24 -05:00
|
|
|
associated_types: Vec::new(),
|
2022-09-20 11:55:07 +00:00
|
|
|
is_const,
|
2013-09-11 21:51:13 -07:00
|
|
|
};
|
2015-05-22 21:10:14 +05:30
|
|
|
trait_def.expand(cx, mitem, item, push)
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-24 17:22:48 +00:00
|
|
|
fn default_call(cx: &ExtCtxt<'_>, span: Span) -> ast::ptr::P<ast::Expr> {
|
|
|
|
|
// Note that `kw::Default` is "default" and `sym::Default` is "Default"!
|
|
|
|
|
let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
|
|
|
|
|
cx.expr_call_global(span, default_ident, ThinVec::new())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 20:22:52 -04:00
|
|
|
fn default_struct_substructure(
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\mod.rs:120:9
|
120 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1573:13
|
1573 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1556:13
|
1556 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1463:13
|
1463 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1433:36
|
1433 | fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:953:13
|
953 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:932:13
|
932 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:580:13
|
580 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:989:13
|
989 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\clone.rs:97:9
|
97 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\cmp\eq.rs:52:9
|
52 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\hash.rs:50:9
|
50 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\encodable.rs:150:9
|
150 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:176:9
|
176 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:106:9
|
106 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:57:9
|
57 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:84:9
|
84 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:212:9
|
212 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:48:26
|
48 | fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 12:16:08 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2019-02-04 21:49:54 +09:00
|
|
|
trait_span: Span,
|
|
|
|
|
substr: &Substructure<'_>,
|
2021-06-29 20:22:52 -04:00
|
|
|
summary: &StaticFields,
|
2022-06-28 13:10:36 +10:00
|
|
|
) -> BlockOrExpr {
|
|
|
|
|
let expr = match summary {
|
2024-02-13 23:31:06 +00:00
|
|
|
Unnamed(_, IsTuple::No) => cx.expr_ident(trait_span, substr.type_ident),
|
|
|
|
|
Unnamed(fields, IsTuple::Yes) => {
|
2024-08-24 17:22:48 +00:00
|
|
|
let exprs = fields.iter().map(|sp| default_call(cx, *sp)).collect();
|
2022-12-06 13:22:36 +00:00
|
|
|
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
2022-12-06 13:22:36 +00:00
|
|
|
Named(fields) => {
|
2021-06-29 20:22:52 -04:00
|
|
|
let default_fields = fields
|
|
|
|
|
.iter()
|
2024-08-24 17:22:48 +00:00
|
|
|
.map(|(ident, span, default_val)| {
|
|
|
|
|
let value = match default_val {
|
|
|
|
|
// We use `Default::default()`.
|
|
|
|
|
None => default_call(cx, *span),
|
|
|
|
|
// We use the field default const expression.
|
|
|
|
|
Some(val) => {
|
|
|
|
|
cx.expr(val.value.span, ast::ExprKind::ConstBlock(val.clone()))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
cx.field_imm(*span, *ident, value)
|
|
|
|
|
})
|
2021-06-29 20:22:52 -04:00
|
|
|
.collect();
|
|
|
|
|
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
|
|
|
|
|
}
|
2022-06-28 13:10:36 +10:00
|
|
|
};
|
|
|
|
|
BlockOrExpr::new_expr(expr)
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_enum_substructure(
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\mod.rs:120:9
|
120 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1573:13
|
1573 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1556:13
|
1556 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1463:13
|
1463 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1433:36
|
1433 | fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:953:13
|
953 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:932:13
|
932 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:580:13
|
580 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:989:13
|
989 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\clone.rs:97:9
|
97 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\cmp\eq.rs:52:9
|
52 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\hash.rs:50:9
|
50 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\encodable.rs:150:9
|
150 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:176:9
|
176 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:106:9
|
106 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:57:9
|
57 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:84:9
|
84 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:212:9
|
212 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:48:26
|
48 | fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 12:16:08 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2021-06-29 20:22:52 -04:00
|
|
|
trait_span: Span,
|
|
|
|
|
enum_def: &EnumDef,
|
2024-12-21 02:43:09 +00:00
|
|
|
item_span: Span,
|
2022-06-28 13:10:36 +10:00
|
|
|
) -> BlockOrExpr {
|
2024-02-25 22:22:11 +01:00
|
|
|
let expr = match try {
|
2024-12-21 02:43:09 +00:00
|
|
|
let default_variant = extract_default_variant(cx, enum_def, trait_span, item_span)?;
|
2024-02-25 22:22:11 +01:00
|
|
|
validate_default_attribute(cx, default_variant)?;
|
|
|
|
|
default_variant
|
|
|
|
|
} {
|
|
|
|
|
Ok(default_variant) => {
|
|
|
|
|
// We now know there is exactly one unit variant with exactly one `#[default]` attribute.
|
2024-08-24 17:22:48 +00:00
|
|
|
match &default_variant.data {
|
|
|
|
|
VariantData::Unit(_) => cx.expr_path(cx.path(
|
|
|
|
|
default_variant.span,
|
|
|
|
|
vec![Ident::new(kw::SelfUpper, default_variant.span), default_variant.ident],
|
|
|
|
|
)),
|
|
|
|
|
VariantData::Struct { fields, .. } => {
|
|
|
|
|
// This only happens if `#![feature(default_field_values)]`. We have validated
|
|
|
|
|
// all fields have default values in the definition.
|
|
|
|
|
let default_fields = fields
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|field| {
|
|
|
|
|
cx.field_imm(
|
|
|
|
|
field.span,
|
|
|
|
|
field.ident.unwrap(),
|
|
|
|
|
match &field.default {
|
|
|
|
|
// We use `Default::default()`.
|
|
|
|
|
None => default_call(cx, field.span),
|
|
|
|
|
// We use the field default const expression.
|
|
|
|
|
Some(val) => cx.expr(
|
|
|
|
|
val.value.span,
|
|
|
|
|
ast::ExprKind::ConstBlock(val.clone()),
|
2025-02-08 22:12:13 +00:00
|
|
|
),
|
2024-08-24 17:22:48 +00:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let path = cx.path(
|
|
|
|
|
default_variant.span,
|
|
|
|
|
vec![
|
|
|
|
|
Ident::new(kw::SelfUpper, default_variant.span),
|
|
|
|
|
default_variant.ident,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
cx.expr_struct(default_variant.span, path, default_fields)
|
|
|
|
|
}
|
|
|
|
|
// Logic error in `extract_default_variant`.
|
|
|
|
|
VariantData::Tuple(..) => {
|
|
|
|
|
cx.dcx().bug("encountered tuple variant annotated with `#[default]`")
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-25 22:22:11 +01:00
|
|
|
}
|
|
|
|
|
Err(guar) => DummyResult::raw_expr(trait_span, Some(guar)),
|
2021-06-29 20:22:52 -04:00
|
|
|
};
|
2022-06-28 13:10:36 +10:00
|
|
|
BlockOrExpr::new_expr(expr)
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extract_default_variant<'a>(
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\mod.rs:120:9
|
120 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1573:13
|
1573 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1556:13
|
1556 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1463:13
|
1463 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1433:36
|
1433 | fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:953:13
|
953 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:932:13
|
932 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:580:13
|
580 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:989:13
|
989 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\clone.rs:97:9
|
97 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\cmp\eq.rs:52:9
|
52 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\hash.rs:50:9
|
50 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\encodable.rs:150:9
|
150 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:176:9
|
176 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:106:9
|
106 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:57:9
|
57 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:84:9
|
84 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:212:9
|
212 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:48:26
|
48 | fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 12:16:08 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2021-06-29 20:22:52 -04:00
|
|
|
enum_def: &'a EnumDef,
|
|
|
|
|
trait_span: Span,
|
2024-12-21 02:43:09 +00:00
|
|
|
item_span: Span,
|
2024-02-25 22:22:11 +01:00
|
|
|
) -> Result<&'a rustc_ast::Variant, ErrorGuaranteed> {
|
2021-06-29 20:22:52 -04:00
|
|
|
let default_variants: SmallVec<[_; 1]> = enum_def
|
|
|
|
|
.variants
|
|
|
|
|
.iter()
|
2023-03-19 21:32:34 +04:00
|
|
|
.filter(|variant| attr::contains_name(&variant.attrs, kw::Default))
|
2021-06-29 20:22:52 -04:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let variant = match default_variants.as_slice() {
|
|
|
|
|
[variant] => variant,
|
|
|
|
|
[] => {
|
2021-07-12 02:26:14 -04:00
|
|
|
let possible_defaults = enum_def
|
|
|
|
|
.variants
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|variant| matches!(variant.data, VariantData::Unit(..)))
|
2023-03-19 21:32:34 +04:00
|
|
|
.filter(|variant| !attr::contains_name(&variant.attrs, sym::non_exhaustive));
|
2021-07-12 02:26:14 -04:00
|
|
|
|
2023-04-08 20:37:41 +01:00
|
|
|
let suggs = possible_defaults
|
2024-12-21 02:43:09 +00:00
|
|
|
.map(|v| errors::NoDefaultVariantSugg { span: v.span.shrink_to_lo() })
|
2023-04-08 20:37:41 +01:00
|
|
|
.collect();
|
2024-12-21 02:43:09 +00:00
|
|
|
let guar =
|
|
|
|
|
cx.dcx().emit_err(errors::NoDefaultVariant { span: trait_span, item_span, suggs });
|
2021-06-29 20:22:52 -04:00
|
|
|
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
[first, rest @ ..] => {
|
2023-04-08 20:37:41 +01:00
|
|
|
let suggs = default_variants
|
|
|
|
|
.iter()
|
2023-11-21 11:43:29 +00:00
|
|
|
.filter_map(|variant| {
|
|
|
|
|
let keep = attr::find_by_name(&variant.attrs, kw::Default)?.span;
|
|
|
|
|
let spans: Vec<Span> = default_variants
|
2023-04-08 20:37:41 +01:00
|
|
|
.iter()
|
2023-11-21 11:43:29 +00:00
|
|
|
.flat_map(|v| {
|
|
|
|
|
attr::filter_by_name(&v.attrs, kw::Default)
|
|
|
|
|
.filter_map(|attr| (attr.span != keep).then_some(attr.span))
|
2023-04-08 20:37:41 +01:00
|
|
|
})
|
|
|
|
|
.collect();
|
2023-11-21 11:43:29 +00:00
|
|
|
(!spans.is_empty())
|
|
|
|
|
.then_some(errors::MultipleDefaultsSugg { spans, ident: variant.ident })
|
2023-04-08 20:37:41 +01:00
|
|
|
})
|
|
|
|
|
.collect();
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().emit_err(errors::MultipleDefaults {
|
2023-04-08 20:37:41 +01:00
|
|
|
span: trait_span,
|
|
|
|
|
first: first.span,
|
|
|
|
|
additional: rest.iter().map(|v| v.span).collect(),
|
|
|
|
|
suggs,
|
|
|
|
|
});
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-24 17:22:48 +00:00
|
|
|
if cx.ecfg.features.default_field_values()
|
|
|
|
|
&& let VariantData::Struct { fields, .. } = &variant.data
|
|
|
|
|
&& fields.iter().all(|f| f.default.is_some())
|
2024-12-06 19:50:55 +00:00
|
|
|
// Disallow `#[default] Variant {}`
|
|
|
|
|
&& !fields.is_empty()
|
2024-08-24 17:22:48 +00:00
|
|
|
{
|
|
|
|
|
// Allowed
|
|
|
|
|
} else if !matches!(variant.data, VariantData::Unit(..)) {
|
2024-12-06 19:50:55 +00:00
|
|
|
let post = if cx.ecfg.features.default_field_values() {
|
|
|
|
|
" or variants where every field has a default value"
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
};
|
|
|
|
|
let guar = cx.dcx().emit_err(errors::NonUnitDefault { span: variant.ident.span, post });
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-19 21:32:34 +04:00
|
|
|
if let Some(non_exhaustive_attr) = attr::find_by_name(&variant.attrs, sym::non_exhaustive) {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().emit_err(errors::NonExhaustiveDefault {
|
2023-04-08 20:37:41 +01:00
|
|
|
span: variant.ident.span,
|
|
|
|
|
non_exhaustive: non_exhaustive_attr.span,
|
|
|
|
|
});
|
2021-06-29 20:22:52 -04:00
|
|
|
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2021-06-29 20:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(variant)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn validate_default_attribute(
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\mod.rs:120:9
|
120 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1573:13
|
1573 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1556:13
|
1556 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1463:13
|
1463 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:1433:36
|
1433 | fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:953:13
|
953 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:932:13
|
932 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:580:13
|
580 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\generic\mod.rs:989:13
|
989 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\clone.rs:97:9
|
97 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\cmp\eq.rs:52:9
|
52 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\hash.rs:50:9
|
50 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\encodable.rs:150:9
|
150 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:176:9
|
176 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:106:9
|
106 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:57:9
|
57 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\default.rs:84:9
|
84 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:212:9
|
212 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\deriving\debug.rs:48:26
|
48 | fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 12:16:08 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2021-06-29 20:22:52 -04:00
|
|
|
default_variant: &rustc_ast::Variant,
|
2024-02-25 22:22:11 +01:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2021-06-29 20:22:52 -04:00
|
|
|
let attrs: SmallVec<[_; 1]> =
|
2023-03-19 21:32:34 +04:00
|
|
|
attr::filter_by_name(&default_variant.attrs, kw::Default).collect();
|
2021-06-29 20:22:52 -04:00
|
|
|
|
|
|
|
|
let attr = match attrs.as_slice() {
|
|
|
|
|
[attr] => attr,
|
2023-12-18 20:54:03 +11:00
|
|
|
[] => cx.dcx().bug(
|
2021-06-29 20:22:52 -04:00
|
|
|
"this method must only be called with a variant that has a `#[default]` attribute",
|
|
|
|
|
),
|
|
|
|
|
[first, rest @ ..] => {
|
2023-04-08 20:37:41 +01:00
|
|
|
let sugg = errors::MultipleDefaultAttrsSugg {
|
|
|
|
|
spans: rest.iter().map(|attr| attr.span).collect(),
|
|
|
|
|
};
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().emit_err(errors::MultipleDefaultAttrs {
|
2023-04-08 20:37:41 +01:00
|
|
|
span: default_variant.ident.span,
|
|
|
|
|
first: first.span,
|
|
|
|
|
first_rest: rest[0].span,
|
|
|
|
|
rest: rest.iter().map(|attr| attr.span).collect::<Vec<_>>().into(),
|
|
|
|
|
only_one: rest.len() == 1,
|
|
|
|
|
sugg,
|
|
|
|
|
});
|
2021-06-29 20:22:52 -04:00
|
|
|
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
2021-06-29 20:22:52 -04:00
|
|
|
};
|
|
|
|
|
if !attr.is_word() {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().emit_err(errors::DefaultHasArg { span: attr.span });
|
2021-06-29 20:22:52 -04:00
|
|
|
|
2024-02-25 22:22:11 +01:00
|
|
|
return Err(guar);
|
2020-03-20 15:03:11 +01:00
|
|
|
}
|
2021-06-29 20:22:52 -04:00
|
|
|
Ok(())
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
2021-07-17 01:05:57 -04:00
|
|
|
|
|
|
|
|
struct DetectNonVariantDefaultAttr<'a, 'b> {
|
|
|
|
|
cx: &'a ExtCtxt<'b>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, 'b> {
|
|
|
|
|
fn visit_attribute(&mut self, attr: &'a rustc_ast::Attribute) {
|
|
|
|
|
if attr.has_name(kw::Default) {
|
2024-12-06 19:50:55 +00:00
|
|
|
let post = if self.cx.ecfg.features.default_field_values() {
|
|
|
|
|
" or variants where every field has a default value"
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
};
|
|
|
|
|
self.cx.dcx().emit_err(errors::NonUnitDefault { span: attr.span, post });
|
2021-07-17 01:05:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rustc_ast::visit::walk_attribute(self, attr);
|
|
|
|
|
}
|
|
|
|
|
fn visit_variant(&mut self, v: &'a rustc_ast::Variant) {
|
2024-10-24 10:41:44 -03:00
|
|
|
self.visit_ident(&v.ident);
|
2021-07-17 01:05:57 -04:00
|
|
|
self.visit_vis(&v.vis);
|
|
|
|
|
self.visit_variant_data(&v.data);
|
2024-05-15 10:01:06 -05:00
|
|
|
visit_opt!(self, visit_anon_const, &v.disr_expr);
|
2021-07-17 01:05:57 -04:00
|
|
|
for attr in &v.attrs {
|
|
|
|
|
rustc_ast::visit::walk_attribute(self, attr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-26 14:59:12 +02:00
|
|
|
|
|
|
|
|
fn has_a_default_variant(item: &Annotatable) -> bool {
|
2024-02-24 02:52:59 -05:00
|
|
|
struct HasDefaultAttrOnVariant;
|
2022-08-26 14:59:12 +02:00
|
|
|
|
|
|
|
|
impl<'ast> rustc_ast::visit::Visitor<'ast> for HasDefaultAttrOnVariant {
|
2024-02-24 02:52:59 -05:00
|
|
|
type Result = ControlFlow<()>;
|
|
|
|
|
fn visit_variant(&mut self, v: &'ast rustc_ast::Variant) -> ControlFlow<()> {
|
2022-08-26 14:59:12 +02:00
|
|
|
if v.attrs.iter().any(|attr| attr.has_name(kw::Default)) {
|
2024-02-24 02:52:59 -05:00
|
|
|
ControlFlow::Break(())
|
|
|
|
|
} else {
|
2024-07-05 15:00:40 +00:00
|
|
|
// no need to walk the variant, we are only looking for top level variants
|
2024-02-24 02:52:59 -05:00
|
|
|
ControlFlow::Continue(())
|
2022-08-26 14:59:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 02:52:59 -05:00
|
|
|
item.visit_with(&mut HasDefaultAttrOnVariant).is_break()
|
2022-08-26 14:59:12 +02:00
|
|
|
}
|