Skip to content

Commit

Permalink
gccrs: Begin enforcing Sized properly and support anti traits like ?S…
Browse files Browse the repository at this point in the history
…ized

Sized is implicitly applied to every generic type parameter apart from the
implicit Self type parameter on traits, as that would cause a recursive
bound and would make Traits unable to be appied to unsized types.

In order to keep everything happy this means we also need to support the
anti trait bound '?' so that Sized can be removed properly. The full Sized
rules are not completely implemented here. Slice's and Str's are currently
marked as Sized but there is a small regression here that looks like a more
general bug which should be fixed on its own as part of #2443. There is
a big testsuite update here to pull in the Sized lang item.

Note this lays the bound's ground work so we can start supporting Drop
trait properly which needs tackled very soon.

Fixes: #2375
Addresses: #2443

gcc/rust/ChangeLog:

	* expand/rust-proc-macro.cc (tokenstream_from_string): update Lexer constructor
	* lex/rust-lex.cc (Lexer::Lexer): likewise
	* lex/rust-lex.h: pass through Linemap
	* parse/rust-cfg-parser.cc (parse_cfg_option): likewise
	* rust-session-manager.cc (Session::load_extern_crate): likewise
	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait):
	dont apply Sized on self
	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::get_marker_predicate): new
	* typecheck/rust-hir-type-check-base.h: add prototype
	* typecheck/rust-hir-type-check-type.cc (TypeResolveGenericParam::Resolve): apply Sized
	(TypeResolveGenericParam::visit): likewise
	* typecheck/rust-hir-type-check-type.h: update prototypes
	* typecheck/rust-typecheck-context.cc: remove bad assertions
	* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_sized_builtin): cleanup
	(TypeCheckBase::get_predicate_from_bound): apply sized rules properly
	* util/rust-hir-map.cc (Mappings::lookup_trait_item_lang_item): helpers
	(Mappings::get_lang_item): likewise
	* util/rust-hir-map.h: likewise
	* util/rust-token-converter.cc (from_ident): update lexer ctor
	(from_punct): likewise

gcc/testsuite/ChangeLog:

	* rust/compile/bounds1.rs: Apply sized trait
	* rust/compile/canonical_paths1.rs: likewise
	* rust/compile/cast_generics.rs: likewise
	* rust/compile/closure_no_type_anno.rs: likewise
	* rust/compile/complex_qualified_path_in_expr.rs: likewise
	* rust/compile/const-issue1440.rs: likewise
	* rust/compile/const_generics_1.rs: likewise
	* rust/compile/const_generics_8.rs: likewise
	* rust/compile/derive_macro1.rs: likewise
	* rust/compile/derive_macro3.rs: likewise
	* rust/compile/derive_macro4.rs: likewise
	* rust/compile/derive_macro6.rs: likewise
	* rust/compile/expected_type_args2.rs: likewise
	* rust/compile/expected_type_args3.rs: likewise
	* rust/compile/generics1.rs: likewise
	* rust/compile/generics11.rs: likewise
	* rust/compile/generics12.rs: likewise
	* rust/compile/generics2.rs: likewise
	* rust/compile/generics3.rs: likewise
	* rust/compile/generics4.rs: likewise
	* rust/compile/generics6.rs: likewise
	* rust/compile/generics7.rs: likewise
	* rust/compile/generics8.rs: likewise
	* rust/compile/if_let_expr.rs: likewise
	* rust/compile/issue-1005.rs: likewise
	* rust/compile/issue-1019.rs: likewise
	* rust/compile/issue-1031.rs: likewise
	* rust/compile/issue-1034.rs: likewise
	* rust/compile/issue-1128.rs: likewise
	* rust/compile/issue-1129-2.rs: likewise
	* rust/compile/issue-1130.rs: likewise
	* rust/compile/issue-1131.rs: likewise
	* rust/compile/issue-1165.rs: likewise
	* rust/compile/issue-1173.rs: likewise
	* rust/compile/issue-1235.rs: likewise
	* rust/compile/issue-1237.rs: likewise
	* rust/compile/issue-1289.rs: likewise
	* rust/compile/issue-1383.rs: likewise
	* rust/compile/issue-1447.rs: likewise
	* rust/compile/issue-1589.rs: likewise
	* rust/compile/issue-1725-1.rs: likewise
	* rust/compile/issue-1725-2.rs: likewise
	* rust/compile/issue-1773.rs: likewise
	* rust/compile/issue-1786.rs: likewise
	* rust/compile/issue-1893.rs: likewise
	* rust/compile/issue-1901.rs: likewise
	* rust/compile/issue-1930.rs: likewise
	* rust/compile/issue-1981.rs: likewise
	* rust/compile/issue-2019-1.rs: likewise
	* rust/compile/issue-2019-2.rs: likewise
	* rust/compile/issue-2019-3.rs: likewise
	* rust/compile/issue-2036.rs: likewise
	* rust/compile/issue-2037.rs: likewise
	* rust/compile/issue-2070.rs: likewise
	* rust/compile/issue-2105.rs: likewise
	* rust/compile/issue-2106.rs: likewise
	* rust/compile/issue-2135.rs: likewise
	* rust/compile/issue-2136-1.rs: likewise
	* rust/compile/issue-2136-2.rs: likewise
	* rust/compile/issue-2139.rs: likewise
	* rust/compile/issue-2142.rs: likewise
	* rust/compile/issue-2165.rs: likewise
	* rust/compile/issue-2166.rs: likewise
	* rust/compile/issue-2190-1.rs: likewise
	* rust/compile/issue-2190-2.rs: likewise
	* rust/compile/issue-2195.rs: likewise
	* rust/compile/issue-2238.rs: likewise
	* rust/compile/issue-2304.rs: likewise
	* rust/compile/issue-850.rs: likewise
	* rust/compile/issue-855.rs: likewise
	* rust/compile/issue-925.rs: likewise
	* rust/compile/macro-issue1400.rs: likewise
	* rust/compile/macro20.rs: likewise
	* rust/compile/macro23.rs: likewise
	* rust/compile/macro40.rs: likewise
	* rust/compile/macro54.rs: likewise
	* rust/compile/name_resolution2.rs: likewise
	* rust/compile/name_resolution4.rs: likewise
	* rust/compile/nested_generic.rs: likewise
	* rust/compile/parse_associated_type_as_generic_arg.rs: likewise
	* rust/compile/parse_associated_type_as_generic_arg2.rs: likewise
	* rust/compile/parse_associated_type_as_generic_arg3.rs: likewise
	* rust/compile/parse_complex_generic_application.rs: likewise
	* rust/compile/parse_complex_generic_application2.rs: likewise
	* rust/compile/path_as_generic_arg.rs: likewise
	* rust/compile/privacy4.rs: likewise
	* rust/compile/privacy6.rs: likewise
	* rust/compile/sizeof-stray-infer-var-bug.rs: likewise
	* rust/compile/stmt_with_block_dot.rs: likewise
	* rust/compile/torture/associated_types1.rs: likewise
	* rust/compile/torture/forward_decl_5.rs: likewise
	* rust/compile/torture/generics1.rs: likewise
	* rust/compile/torture/generics10.rs: likewise
	* rust/compile/torture/generics11.rs: likewise
	* rust/compile/torture/generics12.rs: likewise
	* rust/compile/torture/generics13.rs: likewise
	* rust/compile/torture/generics14.rs: likewise
	* rust/compile/torture/generics15.rs: likewise
	* rust/compile/torture/generics16.rs: likewise
	* rust/compile/torture/generics17.rs: likewise
	* rust/compile/torture/generics18.rs: likewise
	* rust/compile/torture/generics19.rs: likewise
	* rust/compile/torture/generics2.rs: likewise
	* rust/compile/torture/generics20.rs: likewise
	* rust/compile/torture/generics21.rs: likewise
	* rust/compile/torture/generics22.rs: likewise
	* rust/compile/torture/generics23.rs: likewise
	* rust/compile/torture/generics24.rs: likewise
	* rust/compile/torture/generics25.rs: likewise
	* rust/compile/torture/generics26.rs: likewise
	* rust/compile/torture/generics27.rs: likewise
	* rust/compile/torture/generics28.rs: likewise
	* rust/compile/torture/generics29.rs: likewise
	* rust/compile/torture/generics3.rs: likewise
	* rust/compile/torture/generics30.rs: likewise
	* rust/compile/torture/generics31.rs: likewise
	* rust/compile/torture/generics32.rs: likewise
	* rust/compile/torture/generics4.rs: likewise
	* rust/compile/torture/generics5.rs: likewise
	* rust/compile/torture/generics6.rs: likewise
	* rust/compile/torture/generics7.rs: likewise
	* rust/compile/torture/generics8.rs: likewise
	* rust/compile/torture/generics9.rs: likewise
	* rust/compile/torture/intrinsics-2.rs: likewise
	* rust/compile/torture/intrinsics-6.rs: likewise
	* rust/compile/torture/intrinsics-7.rs: likewise
	* rust/compile/torture/intrinsics-8.rs: likewise
	* rust/compile/torture/issue-1024.rs: likewise
	* rust/compile/torture/issue-1075.rs: likewise
	* rust/compile/torture/issue-1432.rs: likewise
	* rust/compile/torture/issue-1555.rs: likewise
	* rust/compile/torture/issue-368.rs: likewise
	* rust/compile/torture/issue-808.rs: likewise
	* rust/compile/torture/issue-862.rs: likewise
	* rust/compile/torture/issue-893-2.rs: likewise
	* rust/compile/torture/issue-893.rs: likewise
	* rust/compile/torture/must_use2.rs: likewise
	* rust/compile/torture/nested_fn2.rs: likewise
	* rust/compile/torture/phantom_data.rs: likewise
	* rust/compile/torture/range-lang-item1.rs: likewise
	* rust/compile/torture/traits1.rs: likewise
	* rust/compile/torture/traits11.rs: likewise
	* rust/compile/torture/traits12.rs: likewise
	* rust/compile/torture/traits13.rs: likewise
	* rust/compile/torture/traits14.rs: likewise
	* rust/compile/torture/traits15.rs: likewise
	* rust/compile/torture/traits16.rs: likewise
	* rust/compile/torture/traits17.rs: likewise
	* rust/compile/torture/traits18.rs: likewise
	* rust/compile/torture/traits19.rs: likewise
	* rust/compile/torture/traits2.rs: likewise
	* rust/compile/torture/traits3.rs: likewise
	* rust/compile/torture/traits4.rs: likewise
	* rust/compile/torture/traits5.rs: likewise
	* rust/compile/torture/traits6.rs: likewise
	* rust/compile/torture/traits7.rs: likewise
	* rust/compile/torture/traits8.rs: likewise
	* rust/compile/torture/traits9.rs: likewise
	* rust/compile/torture/transmute-size-check-1.rs: likewise
	* rust/compile/torture/transmute1.rs: likewise
	* rust/compile/torture/uninit-intrinsic-1.rs: likewise
	* rust/compile/torture/utf8_identifiers.rs: likewise
	* rust/compile/traits1.rs: likewise
	* rust/compile/traits10.rs: likewise
	* rust/compile/traits11.rs: likewise
	* rust/compile/traits12.rs: likewise
	* rust/compile/traits2.rs: likewise
	* rust/compile/traits3.rs: likewise
	* rust/compile/traits4.rs: likewise
	* rust/compile/traits5.rs: likewise
	* rust/compile/traits6.rs: likewise
	* rust/compile/traits7.rs: likewise
	* rust/compile/traits8.rs: likewise
	* rust/compile/traits9.rs: likewise
	* rust/compile/type-bindings1.rs: likewise
	* rust/compile/unconstrained_type_param.rs: likewise
	* rust/compile/unsafe10.rs: likewise
	* rust/execute/torture/closure1.rs: likewise
	* rust/execute/torture/closure2.rs: likewise
	* rust/execute/torture/closure3.rs: likewise
	* rust/execute/torture/closure4.rs: likewise
	* rust/execute/torture/coercion1.rs: likewise
	* rust/execute/torture/coercion2.rs: likewise
	* rust/execute/torture/coercion3.rs: likewise
	* rust/execute/torture/copy_nonoverlapping1.rs: likewise
	* rust/execute/torture/derive_macro1.rs: likewise
	* rust/execute/torture/derive_macro3.rs: likewise
	* rust/execute/torture/derive_macro4.rs: likewise
	* rust/execute/torture/index1.rs: likewise
	* rust/execute/torture/issue-1120.rs: likewise
	* rust/execute/torture/issue-1133.rs: likewise
	* rust/execute/torture/issue-1198.rs: likewise
	* rust/execute/torture/issue-1232.rs: likewise
	* rust/execute/torture/issue-1249.rs: likewise
	* rust/execute/torture/issue-1436.rs: likewise
	* rust/execute/torture/issue-1496.rs: likewise
	* rust/execute/torture/issue-1720-2.rs: likewise
	* rust/execute/torture/issue-1720.rs: likewise
	* rust/execute/torture/issue-2052.rs: likewise
	* rust/execute/torture/issue-2179.rs: likewise
	* rust/execute/torture/issue-2180.rs: likewise
	* rust/execute/torture/issue-2236.rs: likewise
	* rust/execute/torture/issue-647.rs: likewise
	* rust/execute/torture/issue-845.rs: likewise
	* rust/execute/torture/issue-851.rs: likewise
	* rust/execute/torture/issue-858.rs: likewise
	* rust/execute/torture/macros23.rs: likewise
	* rust/execute/torture/macros28.rs: likewise
	* rust/execute/torture/method2.rs: likewise
	* rust/execute/torture/method3.rs: likewise
	* rust/execute/torture/method4.rs: likewise
	* rust/execute/torture/operator_overload_1.rs: likewise
	* rust/execute/torture/operator_overload_10.rs: likewise
	* rust/execute/torture/operator_overload_11.rs: likewise
	* rust/execute/torture/operator_overload_12.rs: likewise
	* rust/execute/torture/operator_overload_2.rs: likewise
	* rust/execute/torture/operator_overload_3.rs: likewise
	* rust/execute/torture/operator_overload_4.rs: likewise
	* rust/execute/torture/operator_overload_5.rs: likewise
	* rust/execute/torture/operator_overload_6.rs: likewise
	* rust/execute/torture/operator_overload_7.rs: likewise
	* rust/execute/torture/operator_overload_8.rs: likewise
	* rust/execute/torture/operator_overload_9.rs: likewise
	* rust/execute/torture/prefetch_data.rs: likewise
	* rust/execute/torture/ref-pattern2.rs: likewise
	* rust/execute/torture/slice-magic.rs: likewise
	* rust/execute/torture/slice-magic2.rs: likewise
	* rust/execute/torture/slice1.rs: likewise
	* rust/execute/torture/str-layout1.rs: likewise
	* rust/execute/torture/trait1.rs: likewise
	* rust/execute/torture/trait10.rs: likewise
	* rust/execute/torture/trait11.rs: likewise
	* rust/execute/torture/trait12.rs: likewise
	* rust/execute/torture/trait13.rs: likewise
	* rust/execute/torture/trait2.rs: likewise
	* rust/execute/torture/trait3.rs: likewise
	* rust/execute/torture/trait4.rs: likewise
	* rust/execute/torture/trait5.rs: likewise
	* rust/execute/torture/trait6.rs: likewise
	* rust/execute/torture/trait7.rs: likewise
	* rust/execute/torture/trait8.rs: likewise
	* rust/execute/torture/trait9.rs: likewise
	* rust/execute/torture/transmute1.rs: likewise
	* rust/execute/torture/wrapping_op1.rs: likewise
	* rust/execute/torture/wrapping_op2.rs: likewise
	* rust/link/generic_function_0.rs: likewise
	* rust/link/generic_function_1.rs: likewise
	* rust/compile/issue-2375.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
  • Loading branch information
philberty committed Jul 29, 2023
1 parent 7fb4619 commit 0c92b2f
Show file tree
Hide file tree
Showing 263 changed files with 924 additions and 54 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/expand/rust-proc-macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ProcMacro::TokenStream
tokenstream_from_string (std::string &data, bool &lex_error)
{
// FIXME: Insert location pointing to call site in tokens
Lexer lex (data);
Lexer lex (data, nullptr);

std::vector<const_TokenPtr> tokens;
TokenPtr ptr;
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/lex/rust-lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ is_identifier_continue (uint32_t codepoint)
return check_xid_property (codepoint) & XID_CONTINUE;
}

Lexer::Lexer (const std::string &input)
Lexer::Lexer (const std::string &input, Linemap *linemap)
: input (RAIIFile::create_error ()), current_line (1), current_column (1),
line_map (nullptr), dump_lex_out ({}),
line_map (linemap), dump_lex_out ({}),
raw_input_source (new BufferInputSource (input, 0)),
input_queue{*raw_input_source}, token_queue (TokenSource (this))
{}
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/lex/rust-lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Lexer
tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt);

// Lex the contents of a string instead of a file
Lexer (const std::string &input);
Lexer (const std::string &input, Linemap *linemap);

// dtor
~Lexer ();
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/parse/rust-cfg-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parse_cfg_option (std::string &input, std::string &key, std::string &value)
key.clear ();
value.clear ();

auto lexer = Lexer (input);
auto lexer = Lexer (input, nullptr);
auto parser = Parser<Lexer> (lexer);

auto token = parser.peek_current_token ();
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus)
mappings->set_current_crate (crate_num);

// then lets parse this as a 2nd crate
Lexer lex (extern_crate.get_metadata ());
Lexer lex (extern_crate.get_metadata (), linemap);
Parser<Lexer> parser (lex);
std::unique_ptr<AST::Crate> metadata_crate = parser.parse_crate ();

Expand Down
20 changes: 13 additions & 7 deletions gcc/rust/typecheck/rust-hir-trait-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
TyTy::BaseType *self = nullptr;
std::vector<TyTy::SubstitutionParamMapping> substitutions;

// FIXME
// this should use the resolve_generic_params like everywhere else
// this needs to be special cased for the sized trait to not auto implemented
// Sized on Self
for (auto &generic_param : trait_reference->get_generic_params ())
{
switch (generic_param.get ()->get_kind ())
Expand All @@ -177,16 +177,22 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
break;

case HIR::GenericParam::GenericKind::TYPE: {
auto &typaram = static_cast<HIR::TypeParam &> (*generic_param);
bool is_self
= typaram.get_type_representation ().as_string ().compare ("Self")
== 0;

// https://doc.rust-lang.org/std/marker/trait.Sized.html
// The one exception is the implicit Self type of a trait
bool apply_sized = !is_self;
auto param_type
= TypeResolveGenericParam::Resolve (generic_param.get ());
= TypeResolveGenericParam::Resolve (generic_param.get (),
apply_sized);
context->insert_type (generic_param->get_mappings (), param_type);

auto &typaram = static_cast<HIR::TypeParam &> (*generic_param);
substitutions.push_back (
TyTy::SubstitutionParamMapping (typaram, param_type));

if (typaram.get_type_representation ().as_string ().compare ("Self")
== 0)
if (is_self)
{
rust_assert (param_type->get_kind () == TyTy::TypeKind::PARAM);
TyTy::ParamType *p
Expand Down
17 changes: 17 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "rust-hir-type-check-base.h"
#include "rust-hir-type-check-expr.h"
#include "rust-hir-type-check-type.h"
#include "rust-hir-trait-resolve.h"
#include "rust-type-util.h"

namespace Rust {
Expand Down Expand Up @@ -399,5 +400,21 @@ TypeCheckBase::resolve_generic_params (
}
}

TyTy::TypeBoundPredicate
TypeCheckBase::get_marker_predicate (Analysis::RustLangItem::ItemType item_type,
location_t locus)
{
DefId item_id = mappings->get_lang_item (item_type, locus);
HIR::Item *item = mappings->lookup_defid (item_id);
rust_assert (item != nullptr);
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);

HIR::Trait &trait = *static_cast<HIR::Trait *> (item);
TraitReference *ref = TraitResolver::Resolve (trait);
rust_assert (ref != nullptr);

return TyTy::TypeBoundPredicate (*ref, BoundPolarity::RegularBound, locus);
}

} // namespace Resolver
} // namespace Rust
8 changes: 7 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class TypeCheckBase
TraitReference *resolve_trait_path (HIR::TypePath &);

TyTy::TypeBoundPredicate
get_predicate_from_bound (HIR::TypePath &path, HIR::Type *associated_self);
get_predicate_from_bound (HIR::TypePath &path, HIR::Type *associated_self,
BoundPolarity polarity
= BoundPolarity::RegularBound);

bool check_for_unconstrained (
const std::vector<TyTy::SubstitutionParamMapping> &params_to_constrain,
Expand All @@ -56,6 +58,10 @@ class TypeCheckBase
const std::vector<std::unique_ptr<HIR::GenericParam> > &generic_params,
std::vector<TyTy::SubstitutionParamMapping> &substitutions);

TyTy::TypeBoundPredicate
get_marker_predicate (Analysis::RustLangItem::ItemType item_type,
Location locus);

Analysis::Mappings *mappings;
Resolver *resolver;
TypeCheckContext *context;
Expand Down
69 changes: 64 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,9 @@ TypeCheckType::visit (HIR::NeverType &type)
}

TyTy::ParamType *
TypeResolveGenericParam::Resolve (HIR::GenericParam *param)
TypeResolveGenericParam::Resolve (HIR::GenericParam *param, bool apply_sized)
{
TypeResolveGenericParam resolver;
TypeResolveGenericParam resolver (apply_sized);
switch (param->get_kind ())
{
case HIR::GenericParam::GenericKind::TYPE:
Expand Down Expand Up @@ -733,8 +733,26 @@ TypeResolveGenericParam::visit (HIR::TypeParam &param)
= new HIR::TypePath (mappings, {}, BUILTINS_LOCATION, false);
}

std::map<DefId, std::vector<TyTy::TypeBoundPredicate>> predicates;

// https://doc.rust-lang.org/std/marker/trait.Sized.html
// All type parameters have an implicit bound of Sized. The special syntax
// ?Sized can be used to remove this bound if it’s not appropriate.
//
// We can only do this when we are not resolving the implicit Self for Sized
// itself
rust_debug_loc (param.get_locus (), "apply_sized: %s",
apply_sized ? "true" : "false");
if (apply_sized)
{
TyTy::TypeBoundPredicate sized_predicate
= get_marker_predicate (Analysis::RustLangItem::ItemType::SIZED,
param.get_locus ());

predicates[sized_predicate.get_id ()] = {sized_predicate};
}

// resolve the bounds
std::vector<TyTy::TypeBoundPredicate> specified_bounds;
if (param.has_type_param_bounds ())
{
for (auto &bound : param.get_type_param_bounds ())
Expand All @@ -747,9 +765,40 @@ TypeResolveGenericParam::visit (HIR::TypeParam &param)

TyTy::TypeBoundPredicate predicate
= get_predicate_from_bound (b->get_path (),
implicit_self_bound);
implicit_self_bound,
b->get_polarity ());
if (!predicate.is_error ())
specified_bounds.push_back (std::move (predicate));
{
switch (predicate.get_polarity ())
{
case BoundPolarity::AntiBound: {
bool found = predicates.find (predicate.get_id ())
!= predicates.end ();
if (found)
predicates.erase (predicate.get_id ());
else
{
// emit error message
rich_location r (line_table, b->get_locus ());
r.add_range (predicate.get ()->get_locus ());
rust_error_at (
r, "antibound for %s is not applied here",
predicate.get ()->get_name ().c_str ());
}
}
break;

default: {
if (predicates.find (predicate.get_id ())
== predicates.end ())
{
predicates[predicate.get_id ()] = {};
}
predicates[predicate.get_id ()].push_back (predicate);
}
break;
}
}
}
break;

Expand All @@ -759,6 +808,16 @@ TypeResolveGenericParam::visit (HIR::TypeParam &param)
}
}

// now to flat map the specified_bounds into the raw specified predicates
std::vector<TyTy::TypeBoundPredicate> specified_bounds;
for (auto it = predicates.begin (); it != predicates.end (); it++)
{
for (const auto &predicate : it->second)
{
specified_bounds.push_back (predicate);
}
}

resolved = new TyTy::ParamType (param.get_type_representation ().as_string (),
param.get_locus (),
param.get_mappings ().get_hirid (), param,
Expand Down
8 changes: 6 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,21 @@ class TypeCheckType : public TypeCheckBase, public HIR::HIRTypeVisitor
class TypeResolveGenericParam : public TypeCheckBase
{
public:
static TyTy::ParamType *Resolve (HIR::GenericParam *param);
static TyTy::ParamType *Resolve (HIR::GenericParam *param,
bool apply_sized = true);

protected:
void visit (HIR::TypeParam &param);
void visit (HIR::LifetimeParam &param);
void visit (HIR::ConstGenericParam &param);

private:
TypeResolveGenericParam () : TypeCheckBase (), resolved (nullptr) {}
TypeResolveGenericParam (bool apply_sized)
: TypeCheckBase (), resolved (nullptr), apply_sized (apply_sized)
{}

TyTy::ParamType *resolved;
bool apply_sized;
};

class ResolveWhereClauseItem : public TypeCheckBase
Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/typecheck/rust-typecheck-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ void
TypeCheckContext::insert_autoderef_mappings (
HirId id, std::vector<Adjustment> &&adjustments)
{
rust_assert (autoderef_mappings.find (id) == autoderef_mappings.end ());
autoderef_mappings.emplace (id, std::move (adjustments));
}

Expand All @@ -365,8 +364,6 @@ void
TypeCheckContext::insert_cast_autoderef_mappings (
HirId id, std::vector<Adjustment> &&adjustments)
{
rust_assert (cast_autoderef_mappings.find (id)
== cast_autoderef_mappings.end ());
cast_autoderef_mappings.emplace (id, std::move (adjustments));
}

Expand Down
48 changes: 31 additions & 17 deletions gcc/rust/typecheck/rust-tyty-bounds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ TypeBoundsProbe::assemble_sized_builtin ()
{
const TyTy::BaseType *raw = receiver->destructure ();

// does this thing actually implement sized?
// https://runrust.miraheze.org/wiki/Dynamically_Sized_Type
// everything is sized except for:
//
// 1. dyn traits
// 2. slices
// 3. str
// 4. ADT's which contain any of the above
// t. tuples which contain any of the above
switch (raw->get_kind ())
{
case TyTy::ADT:
case TyTy::STR:
case TyTy::ARRAY:
case TyTy::REF:
case TyTy::POINTER:
case TyTy::PARAM:
case TyTy::SLICE:
case TyTy::FNDEF:
case TyTy::FNPTR:
case TyTy::TUPLE:
case TyTy::BOOL:
case TyTy::CHAR:
case TyTy::INT:
Expand All @@ -124,13 +128,21 @@ TypeBoundsProbe::assemble_sized_builtin ()
case TyTy::ISIZE:
case TyTy::CLOSURE:
case TyTy::INFER:
assemble_builtin_candidate (Analysis::RustLangItem::SIZED);
break;

case TyTy::ARRAY:
case TyTy::NEVER:
case TyTy::PLACEHOLDER:
case TyTy::PROJECTION:
assemble_builtin_candidate (Analysis::RustLangItem::SIZED);
break;

// FIXME str and slice need to be moved and test cases updated
case TyTy::SLICE:
case TyTy::STR:
case TyTy::ADT:
case TyTy::TUPLE:
// FIXME add extra checks
assemble_builtin_candidate (Analysis::RustLangItem::SIZED);
break;

case TyTy::DYNAMIC:
case TyTy::ERROR:
break;
Expand Down Expand Up @@ -171,7 +183,8 @@ TypeCheckBase::resolve_trait_path (HIR::TypePath &path)

TyTy::TypeBoundPredicate
TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path,
HIR::Type *associated_self)
HIR::Type *associated_self,
BoundPolarity polarity)
{
TyTy::TypeBoundPredicate lookup = TyTy::TypeBoundPredicate::error ();
bool already_resolved
Expand All @@ -184,8 +197,7 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path,
if (trait->is_error ())
return TyTy::TypeBoundPredicate::error ();

TyTy::TypeBoundPredicate predicate (*trait, BoundPolarity::RegularBound,
type_path.get_locus ());
TyTy::TypeBoundPredicate predicate (*trait, polarity, type_path.get_locus ());
HIR::GenericArgs args
= HIR::GenericArgs::create_empty (type_path.get_locus ());

Expand Down Expand Up @@ -234,7 +246,8 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path,
TypeCheckType::Resolve (fn.get_return_type ().get ());

HIR::TraitItem *trait_item = mappings->lookup_trait_item_lang_item (
Analysis::RustLangItem::ItemType::FN_ONCE_OUTPUT);
Analysis::RustLangItem::ItemType::FN_ONCE_OUTPUT,
final_seg->get_locus ());

std::vector<HIR::GenericArgsBinding> bindings;
location_t output_locus = fn.get_return_type ()->get_locus ();
Expand Down Expand Up @@ -609,10 +622,11 @@ TypeBoundPredicate::handle_substitions (
TyTy::BaseType *type = it.second;

TypeBoundPredicateItem item = lookup_associated_item (identifier);
rust_assert (!item.is_error ());

const auto item_ref = item.get_raw_item ();
item_ref->associated_type_set (type);
if (!item.is_error ())
{
const auto item_ref = item.get_raw_item ();
item_ref->associated_type_set (type);
}
}

// FIXME more error handling at some point
Expand Down
Loading

0 comments on commit 0c92b2f

Please sign in to comment.