Skip to content

Commit

Permalink
Attach docs to cstruct/union field members
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Dec 19, 2024
1 parent a57420d commit b35c9bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/compiler/crystal/semantic/type_declaration_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,25 @@ class Crystal::TypeDeclarationVisitor < Crystal::SemanticVisitor
if type.lookup_instance_var?(var_name)
node.raise "#{type.type_desc} #{type} already defines a field named '#{field_name}'"
end

ivar = MetaTypeVar.new(var_name, field_type)
ivar.doc = node.var.as(Var).doc
ivar.owner = type

declare_c_struct_or_union_field(type, field_name, ivar, node.location)
end

def declare_c_struct_or_union_field(type, field_name, var, location)
type.instance_vars[var.name] = var
type.add_def Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location))
type.add_def Def.new(field_name, body: InstanceVar.new(var.name))

setter = Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location)).at(location)
setter.doc = var.doc

getter = Def.new(field_name, body: InstanceVar.new(var.name)).at(location)
getter.doc = var.doc

type.add_def setter
type.add_def getter
end

def declare_instance_var(node, var)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6056,8 +6056,8 @@ module Crystal
skip_statement_end

vars.each do |var|
exps << TypeDeclaration.new(var, type)
.at(var).at_end(type).tap(&.doc=(doc))
var.doc = doc
exps << TypeDeclaration.new(var, type).at(var).at_end(type)
end
end

Expand Down

0 comments on commit b35c9bb

Please sign in to comment.