1 // Copyright 2018 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.
11 use rustc::session::Session;
13 use errors::{Applicability, DiagnosticId, DiagnosticBuilder};
14 use rustc::ty::{Ty, TypeFoldable};
16 pub trait StructuredDiagnostic<'tcx> {
17 fn session(&self) -> &Session;
19 fn code(&self) -> DiagnosticId;
21 fn common(&self) -> DiagnosticBuilder<'tcx>;
23 fn diagnostic(&self) -> DiagnosticBuilder<'tcx> {
24 let err = self.common();
25 if self.session().teach(&self.code()) {
32 fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
36 fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
41 pub struct VariadicError<'tcx> {
48 impl<'tcx> VariadicError<'tcx> {
49 pub fn new(sess: &'tcx Session,
52 cast_ty: &'tcx str) -> VariadicError<'tcx> {
53 VariadicError { sess, span, t, cast_ty }
57 impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> {
58 fn session(&self) -> &Session { self.sess }
60 fn code(&self) -> DiagnosticId {
61 __diagnostic_used!(E0617);
62 DiagnosticId::Error("E0617".to_owned())
65 fn common(&self) -> DiagnosticBuilder<'tcx> {
66 let mut err = if self.t.references_error() {
67 self.sess.diagnostic().struct_dummy()
69 self.sess.struct_span_fatal_with_code(
71 &format!("can't pass `{}` to variadic function", self.t),
75 if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
76 err.span_suggestion_with_applicability(
78 &format!("cast the value to `{}`", self.cast_ty),
79 format!("{} as {}", snippet, self.cast_ty),
80 Applicability::MachineApplicable,
83 err.help(&format!("cast the value to `{}`", self.cast_ty));
88 fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
89 err.note(&format!("certain types, like `{}`, must be cast before passing them to a \
90 variadic function, because of arcane ABI rules dictated by the C \
97 pub struct SizedUnsizedCastError<'tcx> {
104 impl<'tcx> SizedUnsizedCastError<'tcx> {
105 pub fn new(sess: &'tcx Session,
108 cast_ty: String) -> SizedUnsizedCastError<'tcx> {
109 SizedUnsizedCastError { sess, span, expr_ty, cast_ty }
113 impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> {
114 fn session(&self) -> &Session { self.sess }
116 fn code(&self) -> DiagnosticId {
117 __diagnostic_used!(E0607);
118 DiagnosticId::Error("E0607".to_owned())
121 fn common(&self) -> DiagnosticBuilder<'tcx> {
122 if self.expr_ty.references_error() {
123 self.sess.diagnostic().struct_dummy()
125 self.sess.struct_span_fatal_with_code(
127 &format!("cannot cast thin pointer `{}` to fat pointer `{}`",
135 fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
137 "Thin pointers are \"simple\" pointers: they are purely a reference to a
140 Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
141 called DST). DST don't have a statically known size, therefore they can
142 only exist behind some kind of pointers that contain additional
143 information. Slices and trait objects are DSTs. In the case of slices,
144 the additional information the fat pointer holds is their size.
146 To fix this error, don't try to cast directly between thin and fat
149 For more information about casts, take a look at The Book:
150 https://doc.rust-lang.org/book/first-edition/casting-between-types.html");