Skip to content

Commit 98c550e

Browse files
committed
Reinstate the previous compact form of "in this field" errors
1 parent 545fcca commit 98c550e

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ use rustc_trait_selection::traits::{self, misc::can_type_implement_copy};
5757

5858
use crate::nonstandard_style::{method_context, MethodLateContext};
5959

60+
use std::fmt::Write;
61+
6062
// hardwired lints from librustc_middle
6163
pub use rustc_session::lint::builtin::*;
6264

@@ -2496,10 +2498,16 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
24962498
init: InitKind,
24972499
) -> Option<InitError> {
24982500
let field_err = variant.fields.iter().find_map(|field| {
2499-
ty_find_init_error(cx, field.ty(cx.tcx, substs), init).map(|err| {
2500-
InitError::from(format!("in this {descr}"))
2501-
.spanned(cx.tcx.def_span(field.did))
2502-
.nested(err)
2501+
ty_find_init_error(cx, field.ty(cx.tcx, substs), init).map(|mut err| {
2502+
if err.span.is_none() {
2503+
err.span = Some(cx.tcx.def_span(field.did));
2504+
write!(&mut err.message, " (in this {descr})").unwrap();
2505+
err
2506+
} else {
2507+
InitError::from(format!("in this {descr}"))
2508+
.spanned(cx.tcx.def_span(field.did))
2509+
.nested(err)
2510+
}
25032511
})
25042512
});
25052513

src/test/ui/lint/invalid_value.stderr

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ LL | let _val: Wrap<&'static T> = mem::zeroed();
3535
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
3636
|
3737
= note: `Wrap<&T>` must be non-null
38-
note: in this struct field
38+
note: references must be non-null (in this struct field)
3939
--> $DIR/invalid_value.rs:17:18
4040
|
4141
LL | struct Wrap<T> { wrapped: T }
4242
| ^^^^^^^^^^
43-
= note: references must be non-null
4443

4544
error: the type `Wrap<&T>` does not permit being left uninitialized
4645
--> $DIR/invalid_value.rs:58:38
@@ -52,12 +51,11 @@ LL | let _val: Wrap<&'static T> = mem::uninitialized();
5251
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
5352
|
5453
= note: `Wrap<&T>` must be non-null
55-
note: in this struct field
54+
note: references must be non-null (in this struct field)
5655
--> $DIR/invalid_value.rs:17:18
5756
|
5857
LL | struct Wrap<T> { wrapped: T }
5958
| ^^^^^^^^^^
60-
= note: references must be non-null
6159

6260
error: the type `!` does not permit zero-initialization
6361
--> $DIR/invalid_value.rs:65:23
@@ -165,12 +163,11 @@ LL | let _val: Ref = mem::zeroed();
165163
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
166164
|
167165
= note: `Ref` must be non-null
168-
note: in this struct field
166+
note: references must be non-null (in this struct field)
169167
--> $DIR/invalid_value.rs:14:12
170168
|
171169
LL | struct Ref(&'static i32);
172170
| ^^^^^^^^^^^^
173-
= note: references must be non-null
174171

175172
error: the type `Ref` does not permit being left uninitialized
176173
--> $DIR/invalid_value.rs:78:25
@@ -182,12 +179,11 @@ LL | let _val: Ref = mem::uninitialized();
182179
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
183180
|
184181
= note: `Ref` must be non-null
185-
note: in this struct field
182+
note: references must be non-null (in this struct field)
186183
--> $DIR/invalid_value.rs:14:12
187184
|
188185
LL | struct Ref(&'static i32);
189186
| ^^^^^^^^^^^^
190-
= note: references must be non-null
191187

192188
error: the type `fn()` does not permit zero-initialization
193189
--> $DIR/invalid_value.rs:80:26
@@ -221,12 +217,11 @@ LL | let _val: Wrap<fn()> = mem::zeroed();
221217
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
222218
|
223219
= note: `Wrap<fn()>` must be non-null
224-
note: in this struct field
220+
note: function pointers must be non-null (in this struct field)
225221
--> $DIR/invalid_value.rs:17:18
226222
|
227223
LL | struct Wrap<T> { wrapped: T }
228224
| ^^^^^^^^^^
229-
= note: function pointers must be non-null
230225

231226
error: the type `Wrap<fn()>` does not permit being left uninitialized
232227
--> $DIR/invalid_value.rs:84:32
@@ -238,12 +233,11 @@ LL | let _val: Wrap<fn()> = mem::uninitialized();
238233
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
239234
|
240235
= note: `Wrap<fn()>` must be non-null
241-
note: in this struct field
236+
note: function pointers must be non-null (in this struct field)
242237
--> $DIR/invalid_value.rs:17:18
243238
|
244239
LL | struct Wrap<T> { wrapped: T }
245240
| ^^^^^^^^^^
246-
= note: function pointers must be non-null
247241

248242
error: the type `WrapEnum<fn()>` does not permit zero-initialization
249243
--> $DIR/invalid_value.rs:86:36
@@ -255,12 +249,11 @@ LL | let _val: WrapEnum<fn()> = mem::zeroed();
255249
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
256250
|
257251
= note: `WrapEnum<fn()>` must be non-null
258-
note: in this field of the only potentially inhabited enum variant
252+
note: function pointers must be non-null (in this field of the only potentially inhabited enum variant)
259253
--> $DIR/invalid_value.rs:18:28
260254
|
261255
LL | enum WrapEnum<T> { Wrapped(T) }
262256
| ^
263-
= note: function pointers must be non-null
264257

265258
error: the type `WrapEnum<fn()>` does not permit being left uninitialized
266259
--> $DIR/invalid_value.rs:87:36
@@ -272,12 +265,11 @@ LL | let _val: WrapEnum<fn()> = mem::uninitialized();
272265
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
273266
|
274267
= note: `WrapEnum<fn()>` must be non-null
275-
note: in this field of the only potentially inhabited enum variant
268+
note: function pointers must be non-null (in this field of the only potentially inhabited enum variant)
276269
--> $DIR/invalid_value.rs:18:28
277270
|
278271
LL | enum WrapEnum<T> { Wrapped(T) }
279272
| ^
280-
= note: function pointers must be non-null
281273

282274
error: the type `Wrap<(RefPair, i32)>` does not permit zero-initialization
283275
--> $DIR/invalid_value.rs:89:42
@@ -288,18 +280,16 @@ LL | let _val: Wrap<(RefPair, i32)> = mem::zeroed();
288280
| this code causes undefined behavior when executed
289281
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
290282
|
291-
note: in this struct field
283+
note: `RefPair` must be non-null (in this struct field)
292284
--> $DIR/invalid_value.rs:17:18
293285
|
294286
LL | struct Wrap<T> { wrapped: T }
295287
| ^^^^^^^^^^
296-
= note: `RefPair` must be non-null
297-
note: in this struct field
288+
note: references must be non-null (in this struct field)
298289
--> $DIR/invalid_value.rs:15:16
299290
|
300291
LL | struct RefPair((&'static i32, i32));
301292
| ^^^^^^^^^^^^^^^^^^^
302-
= note: references must be non-null
303293

304294
error: the type `Wrap<(RefPair, i32)>` does not permit being left uninitialized
305295
--> $DIR/invalid_value.rs:90:42
@@ -310,18 +300,16 @@ LL | let _val: Wrap<(RefPair, i32)> = mem::uninitialized();
310300
| this code causes undefined behavior when executed
311301
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
312302
|
313-
note: in this struct field
303+
note: `RefPair` must be non-null (in this struct field)
314304
--> $DIR/invalid_value.rs:17:18
315305
|
316306
LL | struct Wrap<T> { wrapped: T }
317307
| ^^^^^^^^^^
318-
= note: `RefPair` must be non-null
319-
note: in this struct field
308+
note: references must be non-null (in this struct field)
320309
--> $DIR/invalid_value.rs:15:16
321310
|
322311
LL | struct RefPair((&'static i32, i32));
323312
| ^^^^^^^^^^^^^^^^^^^
324-
= note: references must be non-null
325313

326314
error: the type `NonNull<i32>` does not permit zero-initialization
327315
--> $DIR/invalid_value.rs:92:34
@@ -344,12 +332,11 @@ LL | let _val: NonNull<i32> = mem::uninitialized();
344332
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
345333
|
346334
= note: `std::ptr::NonNull<i32>` must be non-null
347-
note: in this struct field
335+
note: raw pointers must not be uninitialized (in this struct field)
348336
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
349337
|
350338
LL | pointer: *const T,
351339
| ^^^^^^^^^^^^^^^^^
352-
= note: raw pointers must not be uninitialized
353340

354341
error: the type `(NonZeroU32, i32)` does not permit zero-initialization
355342
--> $DIR/invalid_value.rs:95:39
@@ -372,7 +359,7 @@ LL | let _val: (NonZeroU32, i32) = mem::uninitialized();
372359
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
373360
|
374361
= note: `std::num::NonZeroU32` must be non-null
375-
note: in this struct field
362+
note: integers must not be uninitialized (in this struct field)
376363
--> $SRC_DIR/core/src/num/nonzero.rs:LL:COL
377364
|
378365
LL | / nonzero_integers! {
@@ -383,7 +370,6 @@ LL | | #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable
383370
LL | | #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIs...
384371
LL | | }
385372
| |_^
386-
= note: integers must not be uninitialized
387373
= note: this error originates in the macro `nonzero_integers` (in Nightly builds, run with -Z macro-backtrace for more info)
388374

389375
error: the type `*const dyn Send` does not permit zero-initialization
@@ -470,12 +456,11 @@ LL | let _val: OneFruitNonZero = mem::zeroed();
470456
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
471457
|
472458
= note: `OneFruitNonZero` must be non-null
473-
note: in this field of the only potentially inhabited enum variant
459+
note: `std::num::NonZeroU32` must be non-null (in this field of the only potentially inhabited enum variant)
474460
--> $DIR/invalid_value.rs:39:12
475461
|
476462
LL | Banana(NonZeroU32),
477463
| ^^^^^^^^^^
478-
= note: `std::num::NonZeroU32` must be non-null
479464

480465
error: the type `OneFruitNonZero` does not permit being left uninitialized
481466
--> $DIR/invalid_value.rs:108:37
@@ -487,13 +472,12 @@ LL | let _val: OneFruitNonZero = mem::uninitialized();
487472
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
488473
|
489474
= note: `OneFruitNonZero` must be non-null
490-
note: in this field of the only potentially inhabited enum variant
475+
note: `std::num::NonZeroU32` must be non-null (in this field of the only potentially inhabited enum variant)
491476
--> $DIR/invalid_value.rs:39:12
492477
|
493478
LL | Banana(NonZeroU32),
494479
| ^^^^^^^^^^
495-
= note: `std::num::NonZeroU32` must be non-null
496-
note: in this struct field
480+
note: integers must not be uninitialized (in this struct field)
497481
--> $SRC_DIR/core/src/num/nonzero.rs:LL:COL
498482
|
499483
LL | / nonzero_integers! {
@@ -504,7 +488,6 @@ LL | | #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable
504488
LL | | #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIs...
505489
LL | | }
506490
| |_^
507-
= note: integers must not be uninitialized
508491
= note: this error originates in the macro `nonzero_integers` (in Nightly builds, run with -Z macro-backtrace for more info)
509492

510493
error: the type `bool` does not permit being left uninitialized
@@ -528,12 +511,11 @@ LL | let _val: Wrap<char> = mem::uninitialized();
528511
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
529512
|
530513
= note: `Wrap<char>` must be initialized inside its custom valid range
531-
note: in this struct field
514+
note: characters must be a valid Unicode codepoint (in this struct field)
532515
--> $DIR/invalid_value.rs:17:18
533516
|
534517
LL | struct Wrap<T> { wrapped: T }
535518
| ^^^^^^^^^^
536-
= note: characters must be a valid Unicode codepoint
537519

538520
error: the type `NonBig` does not permit being left uninitialized
539521
--> $DIR/invalid_value.rs:118:28
@@ -545,12 +527,11 @@ LL | let _val: NonBig = mem::uninitialized();
545527
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
546528
|
547529
= note: `NonBig` must be initialized inside its custom valid range
548-
note: in this struct field
530+
note: integers must not be uninitialized (in this struct field)
549531
--> $DIR/invalid_value.rs:23:26
550532
|
551533
LL | pub(crate) struct NonBig(u64);
552534
| ^^^
553-
= note: integers must not be uninitialized
554535

555536
error: the type `Fruit` does not permit being left uninitialized
556537
--> $DIR/invalid_value.rs:121:27
@@ -632,12 +613,11 @@ LL | let _val: WrapAroundRange = mem::uninitialized();
632613
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
633614
|
634615
= note: `WrapAroundRange` must be initialized inside its custom valid range
635-
note: in this struct field
616+
note: integers must not be uninitialized (in this struct field)
636617
--> $DIR/invalid_value.rs:49:35
637618
|
638619
LL | pub(crate) struct WrapAroundRange(u8);
639620
| ^^
640-
= note: integers must not be uninitialized
641621

642622
error: the type `Result<i32, i32>` does not permit being left uninitialized
643623
--> $DIR/invalid_value.rs:144:38
@@ -708,12 +688,11 @@ LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
708688
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
709689
|
710690
= note: `std::ptr::NonNull<i32>` must be non-null
711-
note: in this struct field
691+
note: raw pointers must not be uninitialized (in this struct field)
712692
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
713693
|
714694
LL | pointer: *const T,
715695
| ^^^^^^^^^^^^^^^^^
716-
= note: raw pointers must not be uninitialized
717696

718697
error: the type `bool` does not permit being left uninitialized
719698
--> $DIR/invalid_value.rs:159:26

0 commit comments

Comments
 (0)