1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rustc::session::Session;
use syntax_pos::Span;
use errors::{DiagnosticId, DiagnosticBuilder};
use rustc::ty::{Ty, TypeFoldable};

pub trait StructuredDiagnostic<'tcx> {
    fn session(&self) -> &Session;

    fn code(&self) -> DiagnosticId;

    fn common(&self) -> DiagnosticBuilder<'tcx>;

    fn diagnostic(&self) -> DiagnosticBuilder<'tcx> {
        let err = self.common();
        if self.session().teach(&self.code()) {
            self.extended(err)
        } else {
            self.regular(err)
        }
    }

    fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
        err
    }

    fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
        err
    }
}

pub struct VariadicError<'tcx> {
    sess: &'tcx Session,
    span: Span,
    t: Ty<'tcx>,
    cast_ty: &'tcx str,
}

impl<'tcx> VariadicError<'tcx> {
    pub fn new(sess: &'tcx Session,
               span: Span,
               t: Ty<'tcx>,
               cast_ty: &'tcx str) -> VariadicError<'tcx> {
        VariadicError { sess, span, t, cast_ty }
    }
}

impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> {
    fn session(&self) -> &Session { self.sess }

    fn code(&self) -> DiagnosticId {
        __diagnostic_used!(E0617);
        DiagnosticId::Error("E0617".to_owned())
    }

    fn common(&self) -> DiagnosticBuilder<'tcx> {
        let mut err = if self.t.references_error() {
            self.sess.diagnostic().struct_dummy()
        } else {
            self.sess.struct_span_fatal_with_code(
                self.span,
                &format!("can't pass `{}` to variadic function", self.t),
                self.code(),
            )
        };
        if let Ok(snippet) = self.sess.codemap().span_to_snippet(self.span) {
            err.span_suggestion(self.span,
                                &format!("cast the value to `{}`", self.cast_ty),
                                format!("{} as {}", snippet, self.cast_ty));
        } else {
            err.help(&format!("cast the value to `{}`", self.cast_ty));
        }
        err
    }

    fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
        err.note(&format!("certain types, like `{}`, must be cast before passing them to a \
                           variadic function, because of arcane ABI rules dictated by the C \
                           standard",
                          self.t));
        err
    }
}

pub struct SizedUnsizedCastError<'tcx> {
    sess: &'tcx Session,
    span: Span,
    expr_ty: Ty<'tcx>,
    cast_ty: String,
}

impl<'tcx> SizedUnsizedCastError<'tcx> {
    pub fn new(sess: &'tcx Session,
               span: Span,
               expr_ty: Ty<'tcx>,
               cast_ty: String) -> SizedUnsizedCastError<'tcx> {
        SizedUnsizedCastError { sess, span, expr_ty, cast_ty }
    }
}

impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> {
    fn session(&self) -> &Session { self.sess }

    fn code(&self) -> DiagnosticId {
        __diagnostic_used!(E0607);
        DiagnosticId::Error("E0607".to_owned())
    }

    fn common(&self) -> DiagnosticBuilder<'tcx> {
        if self.expr_ty.references_error() {
            self.sess.diagnostic().struct_dummy()
        } else {
            self.sess.struct_span_fatal_with_code(
                self.span,
                &format!("cannot cast thin pointer `{}` to fat pointer `{}`",
                         self.expr_ty,
                         self.cast_ty),
                self.code(),
            )
        }
    }

    fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
        err.help(
            "Thin pointers are \"simple\" pointers: they are purely a reference to a
memory address.

Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
called DST). DST don't have a statically known size, therefore they can
only exist behind some kind of pointers that contain additional
information. Slices and trait objects are DSTs. In the case of slices,
the additional information the fat pointer holds is their size.

To fix this error, don't try to cast directly between thin and fat
pointers.

For more information about casts, take a look at The Book:
https://doc.rust-lang.org/book/first-edition/casting-between-types.html");
        err
    }
}