1 // Copyright 2012-2014 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.
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.
12 * This module contains a simple utility routine
13 * used by both `typeck` and `const_eval`.
14 * Almost certainly this could (and should) be refactored out of existence.
18 use middle::ty::{self, Ty};
20 use syntax::codemap::Span;
21 use rustc_front::hir as ast;
23 pub fn prohibit_type_params(tcx: &ty::ctxt, segments: &[ast::PathSegment]) {
24 for segment in segments {
25 for typ in segment.parameters.types() {
26 span_err!(tcx.sess, typ.span, E0109,
27 "type parameters are not allowed on this type");
30 for lifetime in segment.parameters.lifetimes() {
31 span_err!(tcx.sess, lifetime.span, E0110,
32 "lifetime parameters are not allowed on this type");
35 for binding in segment.parameters.bindings() {
36 prohibit_projection(tcx, binding.span);
42 pub fn prohibit_projection(tcx: &ty::ctxt, span: Span)
44 span_err!(tcx.sess, span, E0229,
45 "associated type bindings are not allowed here");
48 pub fn prim_ty_to_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
49 segments: &[ast::PathSegment],
52 prohibit_type_params(tcx, segments);
54 ast::TyBool => tcx.types.bool,
55 ast::TyChar => tcx.types.char,
56 ast::TyInt(it) => tcx.mk_mach_int(it),
57 ast::TyUint(uit) => tcx.mk_mach_uint(uit),
58 ast::TyFloat(ft) => tcx.mk_mach_float(ft),
59 ast::TyStr => tcx.mk_str()
63 /// If a type in the AST is a primitive type, return the ty::Ty corresponding
65 pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty)
67 if let ast::TyPath(None, ref path) = ast_ty.node {
68 let def = match tcx.def_map.borrow().get(&ast_ty.id) {
70 tcx.sess.span_bug(ast_ty.span,
71 &format!("unbound path {:?}", path))
73 Some(d) => d.full_def()
75 if let Def::PrimTy(nty) = def {
76 Some(prim_ty_to_ty(tcx, &path.segments, nty))