]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/astconv_util.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / librustc / middle / astconv_util.rs
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
17 use hir::def::Def;
18 use ty::{Ty, TyCtxt};
19
20 use syntax_pos::Span;
21 use hir as ast;
22
23 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24 pub fn prohibit_type_params(self, segments: &[ast::PathSegment]) {
25 for segment in segments {
26 for typ in segment.parameters.types() {
27 struct_span_err!(self.sess, typ.span, E0109,
28 "type parameters are not allowed on this type")
29 .span_label(typ.span, &format!("type parameter not allowed"))
30 .emit();
31 break;
32 }
33 for lifetime in segment.parameters.lifetimes() {
34 struct_span_err!(self.sess, lifetime.span, E0110,
35 "lifetime parameters are not allowed on this type")
36 .span_label(lifetime.span,
37 &format!("lifetime parameter not allowed on this type"))
38 .emit();
39 break;
40 }
41 for binding in segment.parameters.bindings() {
42 self.prohibit_projection(binding.span);
43 break;
44 }
45 }
46 }
47
48 pub fn prohibit_projection(self, span: Span)
49 {
50 let mut err = struct_span_err!(self.sess, span, E0229,
51 "associated type bindings are not allowed here");
52 err.span_label(span, &format!("associate type not allowed here")).emit();
53 }
54
55 pub fn prim_ty_to_ty(self,
56 segments: &[ast::PathSegment],
57 nty: ast::PrimTy)
58 -> Ty<'tcx> {
59 self.prohibit_type_params(segments);
60 match nty {
61 ast::TyBool => self.types.bool,
62 ast::TyChar => self.types.char,
63 ast::TyInt(it) => self.mk_mach_int(it),
64 ast::TyUint(uit) => self.mk_mach_uint(uit),
65 ast::TyFloat(ft) => self.mk_mach_float(ft),
66 ast::TyStr => self.mk_str()
67 }
68 }
69
70 /// If a type in the AST is a primitive type, return the ty::Ty corresponding
71 /// to it.
72 pub fn ast_ty_to_prim_ty(self, ast_ty: &ast::Ty) -> Option<Ty<'tcx>> {
73 if let ast::TyPath(None, ref path) = ast_ty.node {
74 if let Def::PrimTy(nty) = self.expect_def(ast_ty.id) {
75 Some(self.prim_ty_to_ty(&path.segments, nty))
76 } else {
77 None
78 }
79 } else {
80 None
81 }
82 }
83 }