|
| 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. |
| 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 | +use rustc::session::Session; |
| 12 | +use syntax_pos::Span; |
| 13 | +use errors::{DiagnosticId, DiagnosticBuilder}; |
| 14 | +use rustc::ty::{Ty, TypeFoldable}; |
| 15 | + |
| 16 | +pub trait StructuredDiagnostic<'tcx> { |
| 17 | + fn session(&self) -> &Session; |
| 18 | + |
| 19 | + fn code(&self) -> DiagnosticId; |
| 20 | + |
| 21 | + fn common(&self) -> DiagnosticBuilder<'tcx>; |
| 22 | + |
| 23 | + fn diagnostic(&self) -> DiagnosticBuilder<'tcx> { |
| 24 | + let err = self.common(); |
| 25 | + if self.session().explain(&self.code()) { |
| 26 | + self.extended(err) |
| 27 | + } else { |
| 28 | + self.regular(err) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { |
| 33 | + err |
| 34 | + } |
| 35 | + |
| 36 | + fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { |
| 37 | + err |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +pub struct VariadicError<'tcx> { |
| 42 | + sess: &'tcx Session, |
| 43 | + span: Span, |
| 44 | + t: Ty<'tcx>, |
| 45 | + cast_ty: &'tcx str, |
| 46 | +} |
| 47 | + |
| 48 | +impl<'tcx> VariadicError<'tcx> { |
| 49 | + pub fn new(sess: &'tcx Session, |
| 50 | + span: Span, |
| 51 | + t: Ty<'tcx>, |
| 52 | + cast_ty: &'tcx str) -> VariadicError<'tcx> { |
| 53 | + VariadicError { sess, span, t, cast_ty } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> { |
| 58 | + fn session(&self) -> &Session { self.sess } |
| 59 | + |
| 60 | + fn code(&self) -> DiagnosticId { |
| 61 | + __diagnostic_used!(E0617); |
| 62 | + DiagnosticId::Error("E0617".to_owned()) |
| 63 | + } |
| 64 | + |
| 65 | + fn common(&self) -> DiagnosticBuilder<'tcx> { |
| 66 | + let mut err = if self.t.references_error() { |
| 67 | + self.sess.diagnostic().struct_dummy() |
| 68 | + } else { |
| 69 | + self.sess.struct_span_fatal_with_code( |
| 70 | + self.span, |
| 71 | + &format!("can't pass `{}` to variadic function", self.t), |
| 72 | + self.code(), |
| 73 | + ) |
| 74 | + }; |
| 75 | + if let Ok(snippet) = self.sess.codemap().span_to_snippet(self.span) { |
| 76 | + err.span_suggestion(self.span, |
| 77 | + &format!("cast the value to `{}`", self.cast_ty), |
| 78 | + format!("{} as {}", snippet, self.cast_ty)); |
| 79 | + } else { |
| 80 | + err.help(&format!("cast the value to `{}`", self.cast_ty)); |
| 81 | + } |
| 82 | + err |
| 83 | + } |
| 84 | + |
| 85 | + fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { |
| 86 | + err.note(&format!("certain types, like `{}`, must be cast before passing them to a \ |
| 87 | + variadic function, because of arcane ABI rules dictated by the C \ |
| 88 | + standard", |
| 89 | + self.t)); |
| 90 | + err |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +pub struct SizedUnsizedCastError<'tcx> { |
| 95 | + sess: &'tcx Session, |
| 96 | + span: Span, |
| 97 | + expr_ty: Ty<'tcx>, |
| 98 | + cast_ty: String, |
| 99 | +} |
| 100 | + |
| 101 | +impl<'tcx> SizedUnsizedCastError<'tcx> { |
| 102 | + pub fn new(sess: &'tcx Session, |
| 103 | + span: Span, |
| 104 | + expr_ty: Ty<'tcx>, |
| 105 | + cast_ty: String) -> SizedUnsizedCastError<'tcx> { |
| 106 | + SizedUnsizedCastError { sess, span, expr_ty, cast_ty } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> { |
| 111 | + fn session(&self) -> &Session { self.sess } |
| 112 | + |
| 113 | + fn code(&self) -> DiagnosticId { |
| 114 | + __diagnostic_used!(E0607); |
| 115 | + DiagnosticId::Error("E0607".to_owned()) |
| 116 | + } |
| 117 | + |
| 118 | + fn common(&self) -> DiagnosticBuilder<'tcx> { |
| 119 | + if self.expr_ty.references_error() { |
| 120 | + self.sess.diagnostic().struct_dummy() |
| 121 | + } else { |
| 122 | + self.sess.struct_span_fatal_with_code( |
| 123 | + self.span, |
| 124 | + &format!("cannot cast thin pointer `{}` to fat pointer `{}`", |
| 125 | + self.expr_ty, |
| 126 | + self.cast_ty), |
| 127 | + self.code(), |
| 128 | + ) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> { |
| 133 | + err.help( |
| 134 | + "Thin pointers are \"simple\" pointers: they are purely a reference to a |
| 135 | +memory address. |
| 136 | +
|
| 137 | +Fat pointers are pointers referencing \"Dynamically Sized Types\" (also |
| 138 | +called DST). DST don't have a statically known size, therefore they can |
| 139 | +only exist behind some kind of pointers that contain additional |
| 140 | +information. Slices and trait objects are DSTs. In the case of slices, |
| 141 | +the additional information the fat pointer holds is their size. |
| 142 | +
|
| 143 | +To fix this error, don't try to cast directly between thin and fat |
| 144 | +pointers. |
| 145 | +
|
| 146 | +For more information about casts, take a look at The Book: |
| 147 | +https://doc.rust-lang.org/book/first-edition/casting-between-types.html"); |
| 148 | + err |
| 149 | + } |
| 150 | +} |
0 commit comments