Skip to content

Commit 1b8f1fc

Browse files
committed
Do not suggest .into() in consts
1 parent adf2135 commit 1b8f1fc

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/librustc_typeck/check/demand.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use syntax::ast;
1818
use syntax::util::parser::PREC_POSTFIX;
1919
use syntax_pos::{self, Span};
2020
use rustc::hir;
21-
use rustc::hir::print;
2221
use rustc::hir::def::Def;
22+
use rustc::hir::map::NodeItem;
23+
use rustc::hir::{Item, ItemConst, print};
2324
use rustc::ty::{self, Ty, AssociatedItem};
2425
use errors::{DiagnosticBuilder, CodeMapper};
2526

@@ -318,6 +319,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
318319
checked_ty: Ty<'tcx>,
319320
expected_ty: Ty<'tcx>)
320321
-> bool {
322+
let parent_id = self.tcx.hir.get_parent_node(expr.id);
323+
match self.tcx.hir.find(parent_id) {
324+
Some(parent) => {
325+
// Shouldn't suggest `.into()` on `const`s.
326+
if let NodeItem(Item { node: ItemConst(_, _), .. }) = parent {
327+
// FIXME(estebank): modify once we decide to suggest `as` casts
328+
return false;
329+
}
330+
}
331+
None => {}
332+
};
333+
321334
let will_truncate = "will truncate the source value";
322335
let depending_on_isize = "will truncate or zero-extend depending on the bit width of \
323336
`isize`";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 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+
// `const`s shouldn't suggest `.into()`
12+
13+
const TEN: u8 = 10;
14+
const TWELVE: u16 = TEN + 2;
15+
//~^ ERROR mismatched types [E0308]
16+
17+
fn main() {
18+
const TEN: u8 = 10;
19+
const ALSO_TEN: u16 = TEN;
20+
//~^ ERROR mismatched types [E0308]
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/const-type-mismatch.rs:14:21
3+
|
4+
LL | const TWELVE: u16 = TEN + 2;
5+
| ^^^^^^^ expected u16, found u8
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/const-type-mismatch.rs:19:27
9+
|
10+
LL | const ALSO_TEN: u16 = TEN;
11+
| ^^^ expected u16, found u8
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)