]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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. | |
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 | /*! | |
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. | |
15 | */ | |
16 | ||
54a0048b SL |
17 | use hir::def::Def; |
18 | use ty::{Ty, TyCtxt}; | |
1a4d82fc | 19 | |
e9174d1e | 20 | use syntax::codemap::Span; |
54a0048b | 21 | use hir as ast; |
1a4d82fc | 22 | |
54a0048b | 23 | pub fn prohibit_type_params(tcx: &TyCtxt, segments: &[ast::PathSegment]) { |
c34b1796 | 24 | for segment in segments { |
e9174d1e SL |
25 | for typ in segment.parameters.types() { |
26 | span_err!(tcx.sess, typ.span, E0109, | |
27 | "type parameters are not allowed on this type"); | |
28 | break; | |
1a4d82fc | 29 | } |
e9174d1e SL |
30 | for lifetime in segment.parameters.lifetimes() { |
31 | span_err!(tcx.sess, lifetime.span, E0110, | |
32 | "lifetime parameters are not allowed on this type"); | |
33 | break; | |
34 | } | |
35 | for binding in segment.parameters.bindings() { | |
36 | prohibit_projection(tcx, binding.span); | |
37 | break; | |
1a4d82fc JJ |
38 | } |
39 | } | |
40 | } | |
41 | ||
54a0048b | 42 | pub fn prohibit_projection(tcx: &TyCtxt, span: Span) |
e9174d1e SL |
43 | { |
44 | span_err!(tcx.sess, span, E0229, | |
45 | "associated type bindings are not allowed here"); | |
46 | } | |
47 | ||
54a0048b | 48 | pub fn prim_ty_to_ty<'tcx>(tcx: &TyCtxt<'tcx>, |
c34b1796 AL |
49 | segments: &[ast::PathSegment], |
50 | nty: ast::PrimTy) | |
51 | -> Ty<'tcx> { | |
e9174d1e | 52 | prohibit_type_params(tcx, segments); |
c34b1796 AL |
53 | match nty { |
54 | ast::TyBool => tcx.types.bool, | |
55 | ast::TyChar => tcx.types.char, | |
c1a9b12d SL |
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() | |
c34b1796 AL |
60 | } |
61 | } | |
62 | ||
b039eaaf SL |
63 | /// If a type in the AST is a primitive type, return the ty::Ty corresponding |
64 | /// to it. | |
54a0048b | 65 | pub fn ast_ty_to_prim_ty<'tcx>(tcx: &TyCtxt<'tcx>, ast_ty: &ast::Ty) |
1a4d82fc | 66 | -> Option<Ty<'tcx>> { |
c34b1796 AL |
67 | if let ast::TyPath(None, ref path) = ast_ty.node { |
68 | let def = match tcx.def_map.borrow().get(&ast_ty.id) { | |
69 | None => { | |
54a0048b | 70 | span_bug!(ast_ty.span, "unbound path {:?}", path) |
1a4d82fc | 71 | } |
c34b1796 AL |
72 | Some(d) => d.full_def() |
73 | }; | |
7453a54e | 74 | if let Def::PrimTy(nty) = def { |
c34b1796 AL |
75 | Some(prim_ty_to_ty(tcx, &path.segments, nty)) |
76 | } else { | |
77 | None | |
1a4d82fc | 78 | } |
c34b1796 AL |
79 | } else { |
80 | None | |
1a4d82fc JJ |
81 | } |
82 | } |