Skip to content

Commit 1bd6970

Browse files
committed
Account for Self as a type param
1 parent f213acf commit 1bd6970

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::infer::{Subtype, TyCtxtInferExt, ValuePairs};
66
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
77
use rustc_errors::ErrorReported;
88
use rustc_hir as hir;
9+
use rustc_hir::def::Res;
910
use rustc_hir::def_id::DefId;
1011
use rustc_hir::intravisit::Visitor;
1112
use rustc_middle::ty::error::ExpectedFound;
@@ -124,15 +125,17 @@ impl Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
124125

125126
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
126127
match arg.kind {
127-
hir::TyKind::Slice(_) | hir::TyKind::Tup(_) | hir::TyKind::Array(..) => {
128-
hir::intravisit::walk_ty(self, arg);
128+
hir::TyKind::Rptr(_, ref mut_ty) => {
129+
// We don't want to suggest looking into borrowing `&T` or `&Self`.
130+
hir::intravisit::walk_ty(self, mut_ty.ty);
131+
return;
129132
}
130133
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
131134
[segment]
132135
if segment
133136
.res
134137
.map(|res| match res {
135-
hir::def::Res::Def(hir::def::DefKind::TyParam, _) => true,
138+
Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _) => true,
136139
_ => false,
137140
})
138141
.unwrap_or(false) =>
@@ -143,5 +146,6 @@ impl Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
143146
},
144147
_ => {}
145148
}
149+
hir::intravisit::walk_ty(self, arg);
146150
}
147151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::error::Error;
2+
use std::fmt;
3+
4+
#[derive(Copy, Clone, Debug, PartialEq)]
5+
pub enum ValueRef<'a> {
6+
Null,
7+
Integer(i64),
8+
Real(f64),
9+
Text(&'a [u8]),
10+
Blob(&'a [u8]),
11+
}
12+
13+
impl<'a> ValueRef<'a> {
14+
pub fn as_str(&self) -> FromSqlResult<&'a str, &'a &'a str> {
15+
match *self {
16+
ValueRef::Text(t) => {
17+
std::str::from_utf8(t).map_err(|_| FromSqlError::InvalidType).map(|x| (x, &x))
18+
}
19+
_ => Err(FromSqlError::InvalidType),
20+
}
21+
}
22+
}
23+
24+
#[derive(Debug)]
25+
#[non_exhaustive]
26+
pub enum FromSqlError {
27+
InvalidType
28+
}
29+
30+
impl fmt::Display for FromSqlError {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
write!(f, "InvalidType")
33+
}
34+
}
35+
36+
impl Error for FromSqlError {}
37+
38+
pub type FromSqlResult<T, K> = Result<(T, K), FromSqlError>;
39+
40+
pub trait FromSql: Sized {
41+
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
42+
}
43+
44+
impl FromSql for &str {
45+
fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
46+
//~^ ERROR `impl` item signature doesn't match `trait` item signature
47+
value.as_str()
48+
}
49+
}
50+
51+
pub fn main() {
52+
println!("{}", "Hello World");
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: `impl` item signature doesn't match `trait` item signature
2+
--> $DIR/self-without-lifetime-constraint.rs:45:5
3+
|
4+
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
5+
| -------------------------------------------------------------------- expected `fn(ValueRef<'_>) -> std::result::Result<(&str, &&str), FromSqlError>`
6+
...
7+
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'_>) -> std::result::Result<(&str, &&str), FromSqlError>`
9+
|
10+
= note: expected `fn(ValueRef<'_>) -> std::result::Result<(&str, &&str), _>`
11+
found `fn(ValueRef<'_>) -> std::result::Result<(&str, &&str), _>`
12+
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
13+
--> $DIR/self-without-lifetime-constraint.rs:41:60
14+
|
15+
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
16+
| ^^^^ consider borrowing this type parameter in the trait
17+
18+
error: aborting due to previous error
19+

0 commit comments

Comments
 (0)