Skip to content

Commit 7ffb29d

Browse files
committed
Only filter doc(hidden) fields/variants when not crate local
1 parent 04210ae commit 7ffb29d

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,11 @@ impl<'tcx> Constructor<'tcx> {
692692
}
693693

694694
/// Checks if the `Constructor` is a `Constructor::Variant` with a `#[doc(hidden)]`
695-
/// attribute.
695+
/// attribute from a type not local to the current crate.
696696
pub(super) fn is_doc_hidden_variant(&self, pcx: PatCtxt<'_, '_, 'tcx>) -> bool {
697697
if let Constructor::Variant(idx) = self && let ty::Adt(adt, _) = pcx.ty.kind() {
698-
let variant_def_id = adt.variant(*idx).def_id;
699-
return pcx.cx.tcx.is_doc_hidden(variant_def_id);
698+
let variant_def_id = adt.variants[*idx].def_id;
699+
return pcx.cx.tcx.is_doc_hidden(variant_def_id) && !variant_def_id.is_local();
700700
}
701701
false
702702
}

compiler/rustc_typeck/src/check/pat.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13131313
tcx.eval_stability(field.did, None, DUMMY_SP, None),
13141314
EvalResult::Deny { .. }
13151315
)
1316-
&& !tcx.is_doc_hidden(field.did)
1316+
// We only want to report the error if it is hidden and not local
1317+
&& !(tcx.is_doc_hidden(field.did) && !field.did.is_local())
13171318
})
13181319
.collect();
13191320

src/test/ui/pattern/usefulness/doc-hidden-fields.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ extern crate hidden;
44

55
use hidden::HiddenStruct;
66

7+
struct InCrate {
8+
a: usize,
9+
b: bool,
10+
#[doc(hidden)]
11+
im_hidden: u8
12+
}
13+
714
fn main() {
815
let HiddenStruct { one, two, } = HiddenStruct::default();
916
//~^ pattern requires `..` due to inaccessible fields
@@ -13,4 +20,7 @@ fn main() {
1320

1421
let HiddenStruct { one, hide } = HiddenStruct::default();
1522
//~^ pattern does not mention field `two`
23+
24+
let InCrate { a, b } = InCrate { a: 0, b: false, im_hidden: 0 };
25+
//~^ pattern does not mention field `im_hidden`
1626
}

src/test/ui/pattern/usefulness/doc-hidden-fields.stderr

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: pattern requires `..` due to inaccessible fields
2-
--> $DIR/doc-hidden-fields.rs:8:9
2+
--> $DIR/doc-hidden-fields.rs:15:9
33
|
44
LL | let HiddenStruct { one, two, } = HiddenStruct::default();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -10,7 +10,7 @@ LL | let HiddenStruct { one, two, .., } = HiddenStruct::default();
1010
| ++++
1111

1212
error[E0027]: pattern does not mention field `two` and inaccessible fields
13-
--> $DIR/doc-hidden-fields.rs:11:9
13+
--> $DIR/doc-hidden-fields.rs:18:9
1414
|
1515
LL | let HiddenStruct { one, } = HiddenStruct::default();
1616
| ^^^^^^^^^^^^^^^^^^^^^ missing field `two` and inaccessible fields
@@ -25,7 +25,7 @@ LL | let HiddenStruct { one, .. } = HiddenStruct::default();
2525
| ~~~~~~
2626

2727
error[E0027]: pattern does not mention field `two`
28-
--> $DIR/doc-hidden-fields.rs:14:9
28+
--> $DIR/doc-hidden-fields.rs:21:9
2929
|
3030
LL | let HiddenStruct { one, hide } = HiddenStruct::default();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `two`
@@ -39,6 +39,21 @@ help: if you don't care about this missing field, you can explicitly ignore it
3939
LL | let HiddenStruct { one, hide, .. } = HiddenStruct::default();
4040
| ~~~~~~
4141

42-
error: aborting due to 3 previous errors
42+
error[E0027]: pattern does not mention field `im_hidden`
43+
--> $DIR/doc-hidden-fields.rs:24:9
44+
|
45+
LL | let InCrate { a, b } = InCrate { a: 0, b: false, im_hidden: 0 };
46+
| ^^^^^^^^^^^^^^^^ missing field `im_hidden`
47+
|
48+
help: include the missing field in the pattern
49+
|
50+
LL | let InCrate { a, b, im_hidden } = InCrate { a: 0, b: false, im_hidden: 0 };
51+
| ~~~~~~~~~~~~~
52+
help: if you don't care about this missing field, you can explicitly ignore it
53+
|
54+
LL | let InCrate { a, b, .. } = InCrate { a: 0, b: false, im_hidden: 0 };
55+
| ~~~~~~
56+
57+
error: aborting due to 4 previous errors
4358

4459
For more information about this error, try `rustc --explain E0027`.

src/test/ui/pattern/usefulness/doc-hidden-non-exhaustive.rs

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ extern crate hidden;
44

55
use hidden::HiddenEnum;
66

7+
enum InCrate {
8+
A,
9+
B,
10+
#[doc(hidden)]
11+
C,
12+
}
13+
714
fn main() {
815
match HiddenEnum::A {
916
HiddenEnum::A => {}
@@ -27,4 +34,10 @@ fn main() {
2734
Some(HiddenEnum::A) => {}
2835
}
2936
//~^^^^ non-exhaustive patterns: `Some(B)` and `Some(_)` not covered
37+
38+
match InCrate::A {
39+
InCrate::A => {}
40+
InCrate::B => {}
41+
}
42+
//~^^^^ non-exhaustive patterns: `C` not covered
3043
}

src/test/ui/pattern/usefulness/doc-hidden-non-exhaustive.stderr

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0004]: non-exhaustive patterns: `_` not covered
2-
--> $DIR/doc-hidden-non-exhaustive.rs:8:11
2+
--> $DIR/doc-hidden-non-exhaustive.rs:15:11
33
|
44
LL | match HiddenEnum::A {
55
| ^^^^^^^^^^^^^ pattern `_` not covered
@@ -8,7 +8,7 @@ LL | match HiddenEnum::A {
88
= note: the matched value is of type `HiddenEnum`
99

1010
error[E0004]: non-exhaustive patterns: `B` not covered
11-
--> $DIR/doc-hidden-non-exhaustive.rs:14:11
11+
--> $DIR/doc-hidden-non-exhaustive.rs:21:11
1212
|
1313
LL | match HiddenEnum::A {
1414
| ^^^^^^^^^^^^^ pattern `B` not covered
@@ -23,7 +23,7 @@ LL | B,
2323
= note: the matched value is of type `HiddenEnum`
2424

2525
error[E0004]: non-exhaustive patterns: `B` and `_` not covered
26-
--> $DIR/doc-hidden-non-exhaustive.rs:20:11
26+
--> $DIR/doc-hidden-non-exhaustive.rs:27:11
2727
|
2828
LL | match HiddenEnum::A {
2929
| ^^^^^^^^^^^^^ patterns `B` and `_` not covered
@@ -38,7 +38,7 @@ LL | B,
3838
= note: the matched value is of type `HiddenEnum`
3939

4040
error[E0004]: non-exhaustive patterns: `Some(B)` and `Some(_)` not covered
41-
--> $DIR/doc-hidden-non-exhaustive.rs:25:11
41+
--> $DIR/doc-hidden-non-exhaustive.rs:32:11
4242
|
4343
LL | match None {
4444
| ^^^^ patterns `Some(B)` and `Some(_)` not covered
@@ -52,6 +52,24 @@ LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
5252
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5353
= note: the matched value is of type `Option<HiddenEnum>`
5454

55-
error: aborting due to 4 previous errors
55+
error[E0004]: non-exhaustive patterns: `C` not covered
56+
--> $DIR/doc-hidden-non-exhaustive.rs:38:11
57+
|
58+
LL | / enum InCrate {
59+
LL | | A,
60+
LL | | B,
61+
LL | | #[doc(hidden)]
62+
LL | | C,
63+
| | - not covered
64+
LL | | }
65+
| |_- `InCrate` defined here
66+
...
67+
LL | match InCrate::A {
68+
| ^^^^^^^^^^ pattern `C` not covered
69+
|
70+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
71+
= note: the matched value is of type `InCrate`
72+
73+
error: aborting due to 5 previous errors
5674

5775
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)