Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 10 pull requests #64883

Merged
merged 26 commits into from
Sep 29, 2019
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
68d099a
Create new error code E0734 for stability attributes used outside of …
GuillaumeGomez Sep 25, 2019
0ec4513
Fix format macro expansions spans to be macro-generated
rinon Sep 25, 2019
0ebb044
Add long error explanation for E0734
GuillaumeGomez Sep 25, 2019
2e78683
Update ui tests
GuillaumeGomez Sep 25, 2019
ecfe92f
Don't check error_codes files for lints
GuillaumeGomez Sep 27, 2019
ac9aed5
getting more context for duplicate lang items (fixes #60561)
tomtau Sep 27, 2019
f922483
Print ParamTy span when accessing a field (#52082)
Baranowski Sep 26, 2019
d35f25c
Filter out stmts made for the redundant_semicolon lint when pretty-pr…
nathanwhit Sep 12, 2019
66c33c0
Add test for redundant_semicolon lint interaction with proc macro attrs
nathanwhit Sep 28, 2019
800bd3a
data_structures: Add deterministic FxHashMap and FxHashSet wrappers
inashivb Sep 3, 2019
fd505d7
Improve wording in documentation of MaybeUninit
nliberg Sep 27, 2019
f3744a1
Implement CRs
Baranowski Sep 28, 2019
9ad99c3
Refactor into ban_nonexisting_field method
Baranowski Sep 28, 2019
f6cecce
Upgrade async/await to "used" keywords.
ehuss Sep 28, 2019
c666bd5
Fix typo in intrinsics op safety
vertexclique Sep 28, 2019
e77dfa2
Slice docs: fix typo
llogiq Sep 28, 2019
b18d861
Rollup merge of #64131 - shivan1b:deterministic-fxhashmap, r=Mark-Sim…
Centril Sep 28, 2019
55a3ead
Rollup merge of #64387 - nathanwhit:redundant-semi-fix, r=varkor
Centril Sep 28, 2019
05881d0
Rollup merge of #64678 - tomtau:fix/no-std-error, r=matthewjasper
Centril Sep 28, 2019
01075d8
Rollup merge of #64763 - GuillaumeGomez:long-err-explanation-E0734, r…
Centril Sep 28, 2019
d9168e4
Rollup merge of #64793 - immunant:format_spans, r=matthewjasper
Centril Sep 28, 2019
69a3009
Rollup merge of #64837 - nliberg:patch-2, r=Centril
Centril Sep 28, 2019
6145757
Rollup merge of #64852 - Baranowski:param_note_52082, r=estebank
Centril Sep 28, 2019
787829d
Rollup merge of #64875 - ehuss:async-await-reserved, r=estebank
Centril Sep 28, 2019
1c2dd14
Rollup merge of #64876 - vertexclique:vcq/fix-fn-name-intrinsic-op-un…
Centril Sep 28, 2019
4652671
Rollup merge of #64880 - llogiq:slice-docs, r=Centril
Centril Sep 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Print ParamTy span when accessing a field (#52082)
  • Loading branch information
Baranowski committed Sep 27, 2019

Verified

This commit was signed with the committer’s verified signature.
davidtwco David Wood
commit f922483112e407d69c02eb653d7e4bc0ffe2cfa0
34 changes: 31 additions & 3 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
@@ -1393,9 +1393,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut err = self.no_such_field_err(field.span, field, expr_t);

match expr_t.kind {
ty::Adt(def, _) if !def.is_enum() => {
self.suggest_fields_on_recordish(&mut err, def, field);
}
ty::Array(_, len) => {
self.maybe_suggest_array_indexing(&mut err, expr, base, field, len);
}
@@ -1405,6 +1402,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => {}
}

let deref_t = match expr_t.kind {
ty::Ref(_, ref_t, _) => ref_t,
_ => &expr_t
};
match deref_t.kind {
ty::Adt(def, _) if !def.is_enum() => {
self.suggest_fields_on_recordish(&mut err, def, field);
}
ty::Param(param_ty) => {
self.explain_param(&mut err, param_ty);
}
_ => {}
}

if field.name == kw::Await {
// We know by construction that `<expr>.await` is either on Rust 2015
// or results in `ExprKind::Await`. Suggest switching the edition to 2018.
@@ -1491,6 +1502,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.emit();
}

fn explain_param(
&self,
err: &mut DiagnosticBuilder<'_>,
param: ty::ParamTy,
) {
let generics = self.tcx.generics_of(self.body_id.owner_def_id());
let param_def_id = generics.type_param(&param, self.tcx).def_id;
let param_hir_id = match self.tcx.hir().as_local_hir_id(param_def_id) {
Some(x) => x,
None => return,
};
let param_span = self.tcx.hir().span(param_hir_id);
let param_name = self.tcx.hir().ty_param_name(param_hir_id);

err.span_note(param_span, &format!("Type parameter '{}' was declared here", param_name));
}

fn suggest_fields_on_recordish(
&self,
err: &mut DiagnosticBuilder<'_>,
54 changes: 54 additions & 0 deletions src/test/ui/typeck/issue-52082.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Fix issue 52082: Confusing error if accidentially defining a type paramter with the same name as
// an existing type
//
// To this end, make sure that when trying to retrieve a field of a (reference to) type parameter,
// rustc points to the point where the parameter was defined.
#[derive(Debug)]
struct Point
{
x: i32,
y: i32
}

impl Point
{
fn add(a: &Point, b: &Point) -> Point
{
Point {x: a.x + b.x, y: a.y + b.y}
}
}

trait Eq
{
fn equals_ref<T>(a: &T, b: &T) -> bool;
fn equals_val<T>(a: T, b: T) -> bool;
}

impl Eq for Point
{
fn equals_ref<Point>(a: &Point, b: &Point) -> bool
{
a.x == b.x && a.y == b.y //~ ERROR no field `x` on type `&Point` [E0609]
//~|ERROR no field `x` on type `&Point` [E0609]
//~|ERROR no field `y` on type `&Point` [E0609]
//~|ERROR no field `y` on type `&Point` [E0609]
}

fn equals_val<Point>(a: Point, b: Point) -> bool
{
a.x == b.x && a.y == b.y //~ ERROR no field `x` on type `Point` [E0609]
//~|ERROR no field `x` on type `Point` [E0609]
//~|ERROR no field `y` on type `Point` [E0609]
//~|ERROR no field `y` on type `Point` [E0609]
}
}

fn main()
{
let p1 = Point {x: 0, y: 10};
let p2 = Point {x: 20, y: 42};
println!("{:?}", Point::add(&p1, &p2));
println!("p1: {:?}, p2: {:?}", p1, p2);
println!("&p1 == &p2: {:?}", Point::equals_ref(&p1, &p2));
println!("p1 == p2: {:?}", Point::equals_val(p1, p2));
}
99 changes: 99 additions & 0 deletions src/test/ui/typeck/issue-52082.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
error[E0609]: no field `x` on type `&Point`
--> $DIR/issue-52082.rs:31:11
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:29:19
|
LL | fn equals_ref<Point>(a: &Point, b: &Point) -> bool
| ^^^^^

error[E0609]: no field `x` on type `&Point`
--> $DIR/issue-52082.rs:31:18
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:29:19
|
LL | fn equals_ref<Point>(a: &Point, b: &Point) -> bool
| ^^^^^

error[E0609]: no field `y` on type `&Point`
--> $DIR/issue-52082.rs:31:25
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:29:19
|
LL | fn equals_ref<Point>(a: &Point, b: &Point) -> bool
| ^^^^^

error[E0609]: no field `y` on type `&Point`
--> $DIR/issue-52082.rs:31:32
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:29:19
|
LL | fn equals_ref<Point>(a: &Point, b: &Point) -> bool
| ^^^^^

error[E0609]: no field `x` on type `Point`
--> $DIR/issue-52082.rs:39:11
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:37:19
|
LL | fn equals_val<Point>(a: Point, b: Point) -> bool
| ^^^^^

error[E0609]: no field `x` on type `Point`
--> $DIR/issue-52082.rs:39:18
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:37:19
|
LL | fn equals_val<Point>(a: Point, b: Point) -> bool
| ^^^^^

error[E0609]: no field `y` on type `Point`
--> $DIR/issue-52082.rs:39:25
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:37:19
|
LL | fn equals_val<Point>(a: Point, b: Point) -> bool
| ^^^^^

error[E0609]: no field `y` on type `Point`
--> $DIR/issue-52082.rs:39:32
|
LL | a.x == b.x && a.y == b.y
| ^
|
note: Type parameter 'Point' was declared here
--> $DIR/issue-52082.rs:37:19
|
LL | fn equals_val<Point>(a: Point, b: Point) -> bool
| ^^^^^

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0609`.