Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double warning about illegal floating-point literal pattern #86888

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,14 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {

let kind = match cv.ty.kind() {
ty::Float(_) => {
tcx.struct_span_lint_hir(
lint::builtin::ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
id,
span,
|lint| lint.build("floating-point types cannot be used in patterns").emit(),
);
if self.include_lint_checks {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really new to this PR, but it would be nice if include_lint_checks had some documentation on when it's enabled and when not. Currently it seems to be enabled in rustc_mir_build::thir::pattern::MatchVisitor::lower_pattern but not in rustc_mir_build::thir::pattern::pat_from_hir. lower_pattern happens when checking the validity of matches and pat_from_hir happens when actually constructing mir. It might be nice to mention at the field definition site that include_lint_checks will be true when checking validity and false when building mir.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems somewhat wasteful that the ConstToPat lowering even has to run twice (if it didn't, we wouldn't need the include_lint_checks field). However, I don't know the broader design considerations behind all of this, I just developed a targeted fix for the issue you reported, so I'm not the right person to ask for additional documentation here...

tcx.struct_span_lint_hir(
lint::builtin::ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
id,
span,
|lint| lint.build("floating-point types cannot be used in patterns").emit(),
);
}
PatKind::Constant { value: cv }
}
ty::Adt(adt_def, _) if adt_def.is_union() => {
Expand Down
30 changes: 0 additions & 30 deletions src/test/ui/deduplicate-diagnostics-2.deduplicate.stderr

This file was deleted.

39 changes: 0 additions & 39 deletions src/test/ui/deduplicate-diagnostics-2.duplicate.stderr

This file was deleted.

17 changes: 0 additions & 17 deletions src/test/ui/deduplicate-diagnostics-2.rs

This file was deleted.

24 changes: 0 additions & 24 deletions src/test/ui/issues/issue-41255.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,41 @@ fn main() {
match x {
5.0 => {}, //~ ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING this was previously accepted by the compiler but is being
5.0f32 => {}, //~ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING hard error
-5.0 => {}, //~ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING hard error
1.0 .. 33.0 => {}, //~ ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
39.0 ..= 70.0 => {}, //~ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING hard error
//~| WARNING hard error

..71.0 => {}
//~^ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING this was previously accepted by the compiler
..=72.0 => {}
//~^ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING this was previously accepted by the compiler
71.0.. => {}
//~^ ERROR floating-point types cannot be used in patterns
//~| ERROR floating-point types cannot be used in patterns
//~| WARNING hard error
//~| WARNING this was previously accepted by the compiler
_ => {},
};
let y = 5.0;
// Same for tuples
match (x, 5) {
(3.14, 1) => {}, //~ ERROR floating-point types cannot be used
//~| ERROR floating-point types cannot be used
//~| WARNING hard error
//~| WARNING hard error
_ => {},
}
// Or structs
struct Foo { x: f32 };
match (Foo { x }) {
Foo { x: 2.0 } => {}, //~ ERROR floating-point types cannot be used
//~| ERROR floating-point types cannot be used
//~| WARNING hard error
//~| WARNING hard error
_ => {},
}
Expand Down
132 changes: 12 additions & 120 deletions src/test/ui/issues/issue-41255.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | #![forbid(illegal_floating_point_literal_pattern)]
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:15:9
--> $DIR/issue-41255.rs:13:9
|
LL | 5.0f32 => {},
| ^^^^^^
Expand All @@ -22,7 +22,7 @@ LL | 5.0f32 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:19:10
--> $DIR/issue-41255.rs:15:10
|
LL | -5.0 => {},
| ^^^
Expand All @@ -31,7 +31,7 @@ LL | -5.0 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:23:9
--> $DIR/issue-41255.rs:17:9
|
LL | 1.0 .. 33.0 => {},
| ^^^
Expand All @@ -40,7 +40,7 @@ LL | 1.0 .. 33.0 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:23:16
--> $DIR/issue-41255.rs:17:16
|
LL | 1.0 .. 33.0 => {},
| ^^^^
Expand All @@ -49,7 +49,7 @@ LL | 1.0 .. 33.0 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:31:9
--> $DIR/issue-41255.rs:21:9
|
LL | 39.0 ..= 70.0 => {},
| ^^^^
Expand All @@ -58,7 +58,7 @@ LL | 39.0 ..= 70.0 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:31:18
--> $DIR/issue-41255.rs:21:18
|
LL | 39.0 ..= 70.0 => {},
| ^^^^
Expand All @@ -67,7 +67,7 @@ LL | 39.0 ..= 70.0 => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:40:11
--> $DIR/issue-41255.rs:26:11
|
LL | ..71.0 => {}
| ^^^^
Expand All @@ -76,7 +76,7 @@ LL | ..71.0 => {}
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:45:12
--> $DIR/issue-41255.rs:29:12
|
LL | ..=72.0 => {}
| ^^^^
Expand All @@ -85,7 +85,7 @@ LL | ..=72.0 => {}
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:50:9
--> $DIR/issue-41255.rs:32:9
|
LL | 71.0.. => {}
| ^^^^
Expand All @@ -94,7 +94,7 @@ LL | 71.0.. => {}
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:60:10
--> $DIR/issue-41255.rs:40:10
|
LL | (3.14, 1) => {},
| ^^^^
Expand All @@ -103,121 +103,13 @@ LL | (3.14, 1) => {},
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:69:18
--> $DIR/issue-41255.rs:47:18
|
LL | Foo { x: 2.0 } => {},
| ^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:11:9
|
LL | 5.0 => {},
| ^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:15:9
|
LL | 5.0f32 => {},
| ^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:19:10
|
LL | -5.0 => {},
| ^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:23:9
|
LL | 1.0 .. 33.0 => {},
| ^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:23:16
|
LL | 1.0 .. 33.0 => {},
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:31:9
|
LL | 39.0 ..= 70.0 => {},
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:31:18
|
LL | 39.0 ..= 70.0 => {},
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:40:11
|
LL | ..71.0 => {}
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:45:12
|
LL | ..=72.0 => {}
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:50:9
|
LL | 71.0.. => {}
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:60:10
|
LL | (3.14, 1) => {},
| ^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: floating-point types cannot be used in patterns
--> $DIR/issue-41255.rs:69:18
|
LL | Foo { x: 2.0 } => {},
| ^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 </~https://github.com/rust-lang/rust/issues/41620>

error: aborting due to 24 previous errors
error: aborting due to 12 previous errors

Loading