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

Start supporting ?Sized and Sized properly #2483

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions gcc/rust/expand/rust-proc-macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace {
ProcMacro::Literal
literal_from_string (const std::string &data, bool &error)
{
Lexer lex (data);
Lexer lex (data, nullptr);
const_TokenPtr output = lex.build_token ();
if (output == nullptr || !output->is_literal ())
{
Expand All @@ -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
6 changes: 4 additions & 2 deletions gcc/rust/hir/rust-ast-lower-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ ASTLoweringItem::visit (AST::InherentImpl &impl_block)
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
}

Polarity polarity = Positive;
BoundPolarity polarity = BoundPolarity::RegularBound;
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
mapping, std::move (impl_items), std::move (generic_params),
std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
Expand Down Expand Up @@ -683,7 +683,9 @@ ASTLoweringItem::visit (AST::TraitImpl &impl_block)
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
}

Polarity polarity = impl_block.is_exclam () ? Positive : Negative;
BoundPolarity polarity = impl_block.is_exclam ()
? BoundPolarity::RegularBound
: BoundPolarity::NegativeBound;
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
mapping, std::move (impl_items), std::move (generic_params),
std::unique_ptr<HIR::Type> (impl_type),
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/hir/rust-ast-lower-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,11 @@ ASTLoweringTypeBounds::visit (AST::TraitBound &bound)
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

BoundPolarity polarity = bound.has_opening_question_mark ()
? BoundPolarity::AntiBound
: BoundPolarity::RegularBound;
translated = new HIR::TraitBound (mapping, *trait_path, bound.get_locus (),
bound.is_in_parens (),
bound.has_opening_question_mark ());
bound.is_in_parens (), polarity);
}

void
Expand Down
18 changes: 16 additions & 2 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ std::string Dump::delims[2][2] = {
{std::string ("["), std::string ("]")},
};

static std::string
BoundPolarityString (BoundPolarity polarity)
{
switch (polarity)
{
case RegularBound:
return "regular";
case NegativeBound:
return "negative";
case AntiBound:
return "anti";
}
return "unknown";
}

void
Dump::go (HIR::Crate &e)
{
Expand Down Expand Up @@ -2276,8 +2291,7 @@ Dump::visit (TraitBound &e)
begin ("TraitBound");
do_mappings (e.get_mappings ());
put_field ("in_parens", std::to_string (e.get_in_parens ()));
put_field ("opening_question_mark",
std::to_string (e.get_opening_question_mark ()));
put_field ("polarity", BoundPolarityString (e.get_polarity ()));

visit_collection ("for_lifetime", e.get_for_lifetimes ());
visit_field ("type_path", e.get_path ());
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,7 @@ class ImplBlock : public VisItem, public WithInnerAttrs
std::unique_ptr<Type> impl_type;
std::unique_ptr<TypePath> trait_ref;
WhereClause where_clause;
Polarity polarity;
BoundPolarity polarity;
location_t locus;
std::vector<std::unique_ptr<ImplItem>> impl_items;

Expand All @@ -2743,7 +2743,7 @@ class ImplBlock : public VisItem, public WithInnerAttrs
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> impl_type,
std::unique_ptr<TypePath> trait_ref, WhereClause where_clause,
Polarity polarity, Visibility vis, AST::AttrVec inner_attrs,
BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs,
AST::AttrVec outer_attrs, location_t locus)
: VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)),
WithInnerAttrs (std::move (inner_attrs)),
Expand Down Expand Up @@ -2817,7 +2817,7 @@ class ImplBlock : public VisItem, public WithInnerAttrs
bool has_where_clause () const { return !where_clause.is_empty (); }

// Returns the polarity of the impl.
Polarity get_polarity () const { return polarity; }
BoundPolarity get_polarity () const { return polarity; }

location_t get_locus () const override final { return locus; }

Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/hir/tree/rust-hir-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Lifetime;
class TraitBound : public TypeParamBound
{
bool in_parens;
bool opening_question_mark;
BoundPolarity polarity;
std::vector<LifetimeParam> for_lifetimes;
TypePath type_path;
location_t locus;
Expand All @@ -46,10 +46,10 @@ class TraitBound : public TypeParamBound

TraitBound (Analysis::NodeMapping mapping, TypePath type_path,
location_t locus, bool in_parens = false,
bool opening_question_mark = false,
BoundPolarity polarity = BoundPolarity::RegularBound,
std::vector<LifetimeParam> for_lifetimes
= std::vector<LifetimeParam> ())
: in_parens (in_parens), opening_question_mark (opening_question_mark),
: in_parens (in_parens), polarity (polarity),
for_lifetimes (std::move (for_lifetimes)),
type_path (std::move (type_path)), locus (locus), mappings (mapping)
{}
Expand All @@ -67,7 +67,7 @@ class TraitBound : public TypeParamBound

std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
bool get_in_parens () { return in_parens; }
bool get_opening_question_mark () { return opening_question_mark; }
BoundPolarity get_polarity () { return polarity; }

BoundType get_bound_type () const final override { return TRAITBOUND; }

Expand Down
16 changes: 9 additions & 7 deletions gcc/rust/hir/tree/rust-hir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1986,14 +1986,16 @@ TraitBound::as_string () const
{
std::string str ("TraitBound:");

str += "\n Has opening question mark: ";
if (opening_question_mark)
switch (polarity)
{
str += "true";
}
else
{
str += "false";
case RegularBound:
break;
case NegativeBound:
str += "!";
break;
case AntiBound:
str += "?";
break;
}

str += "\n For lifetimes: ";
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
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-hir-path-probe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ PathProbeType::process_associated_trait_for_candidates (
break;
}

const TyTy::TypeBoundPredicate p (*trait_ref, UNDEF_LOCATION);
const TyTy::TypeBoundPredicate p (*trait_ref, BoundPolarity::RegularBound,
UNDEF_LOCATION);
TyTy::TypeBoundPredicateItem item (&p, trait_item_ref);

TyTy::BaseType *trait_item_tyty = item.get_raw_item ()->get_tyty ();
Expand Down
21 changes: 14 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 Expand Up @@ -214,6 +220,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
auto self_hrtb
= TyTy::TypeBoundPredicate (trait_reference->get_mappings ().get_defid (),
std::move (self_subst_copy),
BoundPolarity::RegularBound,
trait_reference->get_locus ());
specified_bounds.push_back (self_hrtb);

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_t locus);

Analysis::Mappings *mappings;
Resolver *resolver;
TypeCheckContext *context;
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,8 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
TraitReference *trait = TraitResolver::Resolve (*trait_item);
rust_assert (!trait->is_error ());

TyTy::TypeBoundPredicate predicate (*trait, expr.get_locus ());
TyTy::TypeBoundPredicate predicate (*trait, BoundPolarity::RegularBound,
expr.get_locus ());

// resolve the trait bound where the <(Args)> are the parameter tuple type
HIR::GenericArgs args = HIR::GenericArgs::create_empty (expr.get_locus ());
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ TypeCheckItem::visit (HIR::Trait &trait)
RustIdent ident{CanonicalPath::create_empty (), trait.get_locus ()};
infered = new TyTy::DynamicObjectType (
trait.get_mappings ().get_hirid (), ident,
{TyTy::TypeBoundPredicate (*trait_ref, trait.get_locus ())});
{TyTy::TypeBoundPredicate (*trait_ref, BoundPolarity::RegularBound,
trait.get_locus ())});
}

void
Expand Down
Loading