Skip to content

Commit 628654b

Browse files
committed
Rollup merge of #27244 - Detegr:master, r=eddyb
Hi all. This is my first contribution to Rust and fixes an issue causing an invalid error message to be presented to the user when using unit struct as length of a repeat expression, issue #27008. The solution is based on suggestions by @oli-obk, but as I'm a complete newbie to this, I have no clue if I got them right :) The biggest concern I have is that if the `NodeId` I'm returning is the correct one or not (it's not meaningful in this case but I think it would be nice to get it right).
2 parents 47c9c49 + e981311 commit 628654b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
967967
Some(def::DefVariant(enum_def, variant_def, _)) => {
968968
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
969969
}
970+
Some(def::DefStruct(_)) => {
971+
return Ok(ConstVal::Struct(e.id))
972+
}
970973
_ => (None, None)
971974
};
972975
let const_expr = match const_expr {

src/test/compile-fail/issue-27008.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct S;
12+
13+
fn main() {
14+
let b = [0; S];
15+
//~^ ERROR mismatched types
16+
//~| expected `usize`
17+
//~| found `S`
18+
//~| expected usize
19+
//~| found struct `S`
20+
//~| ERROR expected positive integer for repeat count, found struct
21+
}

0 commit comments

Comments
 (0)