Skip to content

Commit 31eb5cc

Browse files
committed
Account for const fns to avoid incorrect suggestions
1 parent 4e84b61 commit 31eb5cc

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

src/librustc/hir/map/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,14 @@ impl<'hir> Map<'hir> {
734734
}
735735
}
736736

737+
pub fn is_const_scope(&self, hir_id: HirId) -> bool {
738+
self.walk_parent_nodes(hir_id, |node| match *node {
739+
Node::Item(Item { node: ItemKind::Const(_, _), .. }) => true,
740+
Node::Item(Item { node: ItemKind::Fn(_, header, _, _), .. }) => header.is_const(),
741+
_ => false,
742+
}, |_| false).map(|id| id != CRATE_HIR_ID).unwrap_or(false)
743+
}
744+
737745
/// If there is some error when walking the parents (e.g., a node does not
738746
/// have a parent in the map or a node can't be found), then we return the
739747
/// last good `NodeId` we found. Note that reaching the crate root (`id == 0`),

src/librustc/hir/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,15 @@ pub struct FnHeader {
22882288
pub abi: Abi,
22892289
}
22902290

2291+
impl FnHeader {
2292+
pub fn is_const(&self) -> bool {
2293+
match &self.constness {
2294+
Constness::Const => true,
2295+
_ => false,
2296+
}
2297+
}
2298+
}
2299+
22912300
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
22922301
pub enum ItemKind {
22932302
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.

src/librustc_typeck/check/demand.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax_pos::Span;
77
use rustc::hir;
88
use rustc::hir::def::Def;
99
use rustc::hir::Node;
10-
use rustc::hir::{Item, ItemKind, print};
10+
use rustc::hir::print;
1111
use rustc::ty::{self, Ty, AssociatedItem};
1212
use rustc::ty::adjustment::AllowTwoPhase;
1313
use errors::{Applicability, DiagnosticBuilder};
@@ -550,14 +550,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
550550
checked_ty: Ty<'tcx>,
551551
expected_ty: Ty<'tcx>,
552552
) -> bool {
553-
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
554-
if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {
553+
if self.tcx.hir().is_const_scope(expr.hir_id) {
555554
// Shouldn't suggest `.into()` on `const`s.
556-
if let Node::Item(Item { node: ItemKind::Const(_, _), .. }) = parent {
557-
// FIXME(estebank): modify once we decide to suggest `as` casts
558-
return false;
559-
}
560-
};
555+
// FIXME(estebank): modify once we decide to suggest `as` casts
556+
return false;
557+
}
561558

562559
// If casting this expression to a given numeric type would be appropriate in case of a type
563560
// mismatch.

src/test/ui/numeric/const-scope.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const C: i32 = 1i8; //~ ERROR mismatched types
2+
const D: i8 = C; //~ ERROR mismatched types
3+
4+
const fn foo() {
5+
let c: i32 = 1i8; //~ ERROR mismatched types
6+
let d: i8 = c; //~ ERROR mismatched types
7+
}
8+
9+
fn main() {
10+
let c: i32 = 1i8; //~ ERROR mismatched types
11+
let d: i8 = c; //~ ERROR mismatched types
12+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/const-scope.rs:1:16
3+
|
4+
LL | const C: i32 = 1i8;
5+
| ^^^ expected i32, found i8
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/const-scope.rs:2:15
9+
|
10+
LL | const D: i8 = C;
11+
| ^ expected i8, found i32
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/const-scope.rs:5:18
15+
|
16+
LL | let c: i32 = 1i8;
17+
| ^^^ expected i32, found i8
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/const-scope.rs:6:17
21+
|
22+
LL | let d: i8 = c;
23+
| ^ expected i8, found i32
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/const-scope.rs:10:18
27+
|
28+
LL | let c: i32 = 1i8;
29+
| ^^^ expected i32, found i8
30+
help: change the type of the numeric literal from `i8` to `i32`
31+
|
32+
LL | let c: i32 = 1i32;
33+
| ^^^^
34+
35+
error[E0308]: mismatched types
36+
--> $DIR/const-scope.rs:11:17
37+
|
38+
LL | let d: i8 = c;
39+
| ^ expected i8, found i32
40+
help: you can convert an `i32` to `i8` or panic if it the converted value wouldn't fit
41+
|
42+
LL | let d: i8 = c.try_into().unwrap();
43+
| ^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: aborting due to 6 previous errors
46+
47+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)