Skip to content

Commit 9de4235

Browse files
committed
tychk: Add more support for additional trait bounds in functions
This commit correctly lowers and typechecks parenthesized types, which are used for trait objects with additional bounds. gcc/rust/ChangeLog: * resolve/rust-ast-resolve-type.cc (ResolveType::visit): New visitor to handle ParenthesizedType. * resolve/rust-ast-resolve-type.h: Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise. * typecheck/rust-hir-type-check-type.h: Likewise. gcc/testsuite/ChangeLog: * rust/compile/auto_traits2.rs: New test. * rust/compile/auto_traits3.rs: New test. * rust/compile/nr2/exclude: Add auto_traits2 test.
1 parent 31d3f55 commit 9de4235

File tree

7 files changed

+78
-3
lines changed

7 files changed

+78
-3
lines changed

gcc/rust/resolve/rust-ast-resolve-type.cc

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ ResolveType::visit (AST::TraitObjectType &type)
5050
}
5151
}
5252

53+
void
54+
ResolveType::visit (AST::ParenthesisedType &type)
55+
{
56+
resolved_node = ResolveType::go (*type.get_type_in_parens ());
57+
}
58+
5359
void
5460
ResolveType::visit (AST::ReferenceType &type)
5561
{

gcc/rust/resolve/rust-ast-resolve-type.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rust-diagnostics.h"
2525
#include "rust-hir-map.h"
2626
#include "rust-path.h"
27+
#include "rust-type.h"
2728
#include "util/rust-hir-map.h"
2829

2930
namespace Rust {
@@ -143,6 +144,8 @@ class ResolveType : public ResolverBase
143144

144145
void visit (AST::TraitObjectType &type) override;
145146

147+
void visit (AST::ParenthesisedType &type) override;
148+
146149
void visit (AST::SliceType &type) override;
147150

148151
private:

gcc/rust/typecheck/rust-hir-type-check-type.cc

+6
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,12 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
790790
std::move (specified_bounds));
791791
}
792792

793+
void
794+
TypeCheckType::visit (HIR::ParenthesisedType &type)
795+
{
796+
translated = TypeCheckType::Resolve (type.get_type_in_parens ());
797+
}
798+
793799
void
794800
TypeCheckType::visit (HIR::ArrayType &type)
795801
{

gcc/rust/typecheck/rust-hir-type-check-type.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TypeCheckType : public TypeCheckBase, public HIR::HIRTypeVisitor
5959
void visit (HIR::InferredType &type) override;
6060
void visit (HIR::NeverType &type) override;
6161
void visit (HIR::TraitObjectType &type) override;
62+
void visit (HIR::ParenthesisedType &type) override;
6263

6364
void visit (HIR::TypePathSegmentFunction &segment) override
6465
{ /* TODO */
@@ -69,9 +70,6 @@ class TypeCheckType : public TypeCheckBase, public HIR::HIRTypeVisitor
6970
void visit (HIR::ImplTraitType &type) override
7071
{ /* TODO */
7172
}
72-
void visit (HIR::ParenthesisedType &type) override
73-
{ /* TODO */
74-
}
7573
void visit (HIR::ImplTraitTypeOneBound &type) override
7674
{ /* TODO */
7775
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![feature(optin_builtin_traits)]
2+
3+
pub unsafe auto trait Send {}
4+
#[lang = "sync"]
5+
pub unsafe auto trait Sync {}
6+
7+
trait A {
8+
fn a_method(&self) {}
9+
}
10+
11+
fn foo(a: &(dyn A + Send + Sync)) {
12+
a.a_method();
13+
}
14+
15+
struct S;
16+
17+
impl A for S {
18+
fn a_method(&self) {}
19+
}
20+
21+
fn main() {
22+
let s = S;
23+
24+
foo(&s); // { dg-error "bounds not satisfied" }
25+
// { dg-error "mismatched type" "" { target *-*-* } .-1 }
26+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(optin_builtin_traits)]
2+
3+
pub unsafe auto trait Send {}
4+
#[lang = "sync"]
5+
pub unsafe auto trait Sync {}
6+
7+
trait A {
8+
fn a_method(&self) {}
9+
}
10+
11+
fn foo(a: &(dyn A + Send + Sync)) {
12+
a.a_method();
13+
}
14+
15+
struct S;
16+
17+
impl A for S {
18+
fn a_method(&self) {} // { dg-warning "unused" }
19+
}
20+
21+
// These should not be necessary because they are both auto traits
22+
// They need to be removed once we figure out the proper implementation for each of them
23+
// However, it'd be silly to implement other traits in order to ensure the test is okay,
24+
// as these extra trait bounds are only allowed to use auto traits
25+
// FIXME: #3327
26+
// FIXME: #3326
27+
unsafe impl Send for S {}
28+
unsafe impl Sync for S {}
29+
30+
fn main() {
31+
let s = S;
32+
33+
foo(&s);
34+
}

gcc/testsuite/rust/compile/nr2/exclude

+2
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,6 @@ issue-2907.rs
207207
issue-2423.rs
208208
issue-266.rs
209209
additional-trait-bounds2.rs
210+
auto_traits2.rs
211+
auto_traits3.rs
210212
# please don't delete the trailing newline

0 commit comments

Comments
 (0)