Skip to content

Commit

Permalink
Rollup merge of #71813 - ecstatic-morse:issue-71734, r=tmandry
Browse files Browse the repository at this point in the history
Decode qualifs for associated const defaults

Fixes #71734.

We encode qualifs for associated constants, but never expected to decode the qualifs for defaulted associated consts. Fix this, and test that associated const defaults have the correct qualifs cross-crate.

r? @tmandry
  • Loading branch information
Dylan-DPC committed May 3, 2020
2 parents 2231941 + 14a2c8d commit e48a7b8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,11 +1123,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
match self.kind(id) {
EntryKind::Const(qualif, _)
| EntryKind::AssocConst(
AssocContainer::ImplDefault | AssocContainer::ImplFinal,
AssocContainer::ImplDefault
| AssocContainer::ImplFinal
| AssocContainer::TraitWithDefault,
qualif,
_,
) => qualif,
_ => bug!(),
_ => bug!("mir_const_qualif: unexpected kind"),
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/consts/const_in_pattern/auxiliary/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ impl PartialEq for CustomEq {

pub const NONE: Option<CustomEq> = None;
pub const SOME: Option<CustomEq> = Some(CustomEq);

pub trait AssocConst {
const NONE: Option<CustomEq> = None;
const SOME: Option<CustomEq> = Some(CustomEq);
}
12 changes: 12 additions & 0 deletions src/test/ui/consts/const_in_pattern/cross-crate-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

extern crate consts;

struct Defaulted;
impl consts::AssocConst for Defaulted {}

fn main() {
let _ = Defaulted;
match None {
consts::SOME => panic!(),
//~^ must be annotated with `#[derive(PartialEq, Eq)]`
//~| must be annotated with `#[derive(PartialEq, Eq)]`

_ => {}
}

match None {
<Defaulted as consts::AssocConst>::SOME => panic!(),
//~^ must be annotated with `#[derive(PartialEq, Eq)]`
//~| must be annotated with `#[derive(PartialEq, Eq)]`

_ => {}
}
}
18 changes: 15 additions & 3 deletions src/test/ui/consts/const_in_pattern/cross-crate-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
error: to use a constant of type `consts::CustomEq` in a pattern, `consts::CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/cross-crate-fail.rs:9:9
--> $DIR/cross-crate-fail.rs:13:9
|
LL | consts::SOME => panic!(),
| ^^^^^^^^^^^^

error: to use a constant of type `consts::CustomEq` in a pattern, `consts::CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/cross-crate-fail.rs:9:9
--> $DIR/cross-crate-fail.rs:21:9
|
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: to use a constant of type `consts::CustomEq` in a pattern, `consts::CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/cross-crate-fail.rs:13:9
|
LL | consts::SOME => panic!(),
| ^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: to use a constant of type `consts::CustomEq` in a pattern, `consts::CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/cross-crate-fail.rs:21:9
|
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

9 changes: 9 additions & 0 deletions src/test/ui/consts/const_in_pattern/cross-crate-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
extern crate consts;
use consts::CustomEq;

struct Defaulted;
impl consts::AssocConst for Defaulted {}

fn main() {
let _ = Defaulted;
match Some(CustomEq) {
consts::NONE => panic!(),
_ => {}
}

match Some(CustomEq) {
<Defaulted as consts::AssocConst>::NONE => panic!(),
_ => {}
}
}

0 comments on commit e48a7b8

Please sign in to comment.