]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/astconv_util.rs
New upstream version 1.16.0+dfsg1
[rustc.git] / src / librustc / middle / astconv_util.rs
CommitLineData
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
476ff2be 17use hir;
54a0048b
SL
18use hir::def::Def;
19use ty::{Ty, TyCtxt};
1a4d82fc 20
3157f602 21use syntax_pos::Span;
1a4d82fc 22
a7813a04 23impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
476ff2be 24 pub fn prohibit_type_params(self, segments: &[hir::PathSegment]) {
a7813a04
XL
25 for segment in segments {
26 for typ in segment.parameters.types() {
5bcae85e
SL
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();
a7813a04
XL
31 break;
32 }
33 for lifetime in segment.parameters.lifetimes() {
5bcae85e
SL
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();
a7813a04
XL
39 break;
40 }
41 for binding in segment.parameters.bindings() {
42 self.prohibit_projection(binding.span);
43 break;
44 }
1a4d82fc
JJ
45 }
46 }
1a4d82fc 47
a7813a04
XL
48 pub fn prohibit_projection(self, span: Span)
49 {
5bcae85e
SL
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();
a7813a04 53 }
e9174d1e 54
a7813a04 55 pub fn prim_ty_to_ty(self,
476ff2be
SL
56 segments: &[hir::PathSegment],
57 nty: hir::PrimTy)
a7813a04
XL
58 -> Ty<'tcx> {
59 self.prohibit_type_params(segments);
60 match nty {
476ff2be
SL
61 hir::TyBool => self.types.bool,
62 hir::TyChar => self.types.char,
63 hir::TyInt(it) => self.mk_mach_int(it),
64 hir::TyUint(uit) => self.mk_mach_uint(uit),
65 hir::TyFloat(ft) => self.mk_mach_float(ft),
66 hir::TyStr => self.mk_str()
a7813a04 67 }
c34b1796 68 }
c34b1796 69
a7813a04
XL
70 /// If a type in the AST is a primitive type, return the ty::Ty corresponding
71 /// to it.
476ff2be
SL
72 pub fn ast_ty_to_prim_ty(self, ast_ty: &hir::Ty) -> Option<Ty<'tcx>> {
73 if let hir::TyPath(hir::QPath::Resolved(None, ref path)) = ast_ty.node {
74 if let Def::PrimTy(nty) = path.def {
a7813a04
XL
75 Some(self.prim_ty_to_ty(&path.segments, nty))
76 } else {
77 None
1a4d82fc 78 }
c34b1796
AL
79 } else {
80 None
1a4d82fc 81 }
1a4d82fc
JJ
82 }
83}